亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

利用python編寫爬蟲爬取淘寶奶粉部分數據.1

標簽:
Python

import requests
import re
import pymysql.cursors

def getHTMLText(url):
kv = {'User-Agent':'Mozilla/5.0'} #反反爬虫
try:
r = requests.get(url, headers = kv, timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
print("error1")

def parsePage(ilt,html):
try:

从字符串类型的html中用正则表达式查找相关数据项
    price = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
    title = re.findall(r'\"raw_title\"\:\".*?\"',html)
    place = re.findall(r'"item_loc":".*?"',html)
    for i in range(len(price)):
        #从查找的相关数据项中分离数据,可以在另一交互界面检查分离数据的数据类型
        pprice = eval(price[i].split(':')[1])
        ttitle = eval(title[i].split(':')[1])
        pplace = eval(place[i].split(':')[1])
        ilt.append([pprice,ttitle,pplace])
except:
        print("error2")

def printGoodsList(ilt):
tplt = "{:^4}\t{:8}\t{:^26}\t{:^80}"
print(tplt.format("序号","价格","商品名称","所属地"))
count = 0
num = 0
for g in ilt:
count = count +1
print(tplt.format(count,g[0],g[1],g[2]))

连接数据库(表事先已经建立好)
connect = pymysql.Connect(host="localhost",user="root",password="1984ispb",db="TianmaoMilkPowder",port=3306,charset="utf8")
cursor = connect.cursor()
for g in ilt:
    num =num + 1
    sql = "insert into Powderdata(No,Price,goodsName,Place) values ('%d','%s','%s','%s')"
    data = (num,g[0],g[1],g[2])
    try:
        #执行插入操作
        cursor.execute(sql % data)
        connect.commit()
    except:
        print("错误")
connect.commit()
cursor.close()
print('成功插入', cursor.rowcount, '条数据')

def main():
godds = '奶粉'
depth = 10
start_url = https://s.taobao.com/search?q=goods
infoList = []
for i in range(depth):
try:

换页操作
        url = start_url + '&s=' + str(44*i)
        html = getHTMLText(url)
        parsePage(infoList,html)
    except:
        continue
printGoodsList(infoList)

main()

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消