国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

把爬取到的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中

2023-03-31 09:55 作者:寒冰之雪  | 我要投稿

import pymysql
import re
import requests

#爬取源代碼
url='https://search.jd.com/Search?keyword=ctf&enc=utf-8&wq=ctf'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0'}
res=requests.get(url,headers=headers).text

def 過濾出需要的數(shù)據(jù)(res):
??? s1=re.findall('<em>(.*?)</em>',res,re.S)
??? li=[] #li用于存放商品名稱,s1用于存放商品價(jià)格
??? s2=re.findall('<i data-price=.*?>(.*?)</i>',res,re.S)
??? for i in s1:
??????? st=''
??????? if '¥' in i and len(i)>1:
??????????? st=i
??????? if '<font' in i:
????????? st=''.join(''.join(i.split(re.findall('<font.*?>',i)[0])).split('</font>'))
??????? if '<img' in i:
??????????? st=st.strip('<img'+re.findall('<img(.*?)/>',st)[0]+'/>')
??????? if '<span' in i:
??????????? st=''.join(''.join(st.split(re.findall('<span.*?>',st)[0])).split('</span>'))
??????? li.append(st.strip())
??? lis=[]
??? for i in li: #去除li里的空格
??????? if len(i)>1:
??????????? lis.append(i)
??? return lis,s2

def MySQL操作(lis,s2):
??? db=pymysql.connect(host='localhost',port=3306,user='root',password='',charset='utf8')
??? cur=db.cursor()
??? cur.execute("create database 商品查詢系統(tǒng) character set utf8")
??? cur.execute('use 商品查詢系統(tǒng)')
??? cur.execute('create table ctf書籍(書名 varchar(100),價(jià)格 varchar(100))')
??? for i in range(len(s2)):
??????? sql="insert into ctf書籍(書名,價(jià)格) values ('%s','%s')"%(lis[i],s2[i])
??????? cur.execute(sql)
??? db.commit()
??? x=1?? ?
??? while x!='exit':
??????? x=input('輸入書名:')
??????? sql=f"select * from ctf書籍 where 書名 like '%{x}%'"
??????? cur.execute(sql)
??????? data=cur.fetchall()
??????? for i in data:
??????????? print(i[0],i[1])
??? cur.close()
??? db.close()
?? ?
x=過濾出需要的數(shù)據(jù)(res)
MySQL操作(x[0], x[1])


鏈接:https://www.wolai.com/hbzx/aDPcfF7ePrfHAPMXCZeSc9


把爬取到的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
九江市| 东乌| 永顺县| 枝江市| 吴忠市| 浮山县| 莱州市| 南溪县| 兴城市| 临江市| 大竹县| 苏尼特左旗| 新绛县| 满城县| 太谷县| 长垣县| 莱阳市| 乌鲁木齐市| 石城县| 宕昌县| 彝良县| 玉林市| 思南县| 斗六市| 凤冈县| 武穴市| 黄石市| 嘉善县| 临汾市| 峨边| 贵阳市| 郓城县| 阜城县| 贞丰县| 新田县| 新乐市| 德保县| 加查县| 浮梁县| 大荔县| 宿迁市|