Python,自己修改的爬去淘寶網頁的程式碼 解決Python爬蟲爬取淘寶商品資訊也不報錯,也不輸出資訊
阿新 • • 發佈:2019-01-03
程式碼部分:
下面是正確的:
import requests import re def getHTMLText(url): try: r = requests.get(url, timeout = 30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return "" def parsePage(ilt, html): try: plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html) fee = re.findall(r'\"view_fee\"\:\"[\d\.]*\"',html) xiao = re.findall(r'\"view_sales\"\:\".*?\"',html) tlt = re.findall(r'\"raw_title\"\:\".*?\"',html) shop = re.findall(r'\"nick\"\:\".*?\"',html) for i in range(len(plt)): price = eval(plt[i].split(':')[1]) feeprice = eval(fee[i].split(':')[1]) xiaoliang = eval(xiao[i].split(':')[1]) title = eval(tlt[i].split(':')[1]) shopname = eval(shop[i].split(':')[1]) ilt.append([price,feeprice,xiaoliang,title,shopname])#,sal except: print("") def printGoodsList(ilt): tplt = "{:4}\t{:8}\t{:8}\t{:8}\t{:16}\t{:8}" print(tplt.format("序號","價格","郵費","銷量","商品名稱 ","店鋪名稱")) count = 0 for g in ilt: count = count + 1 print(tplt.format(count,g[0],g[1],g[2],g[3],g[4])) def main(): goods = '書包' depth = 1 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()
這個爬取的正則表示式:
其中在爬取銷量那個標籤的時候,沒有報錯。
但是也不會出現自己想要的東西。其他的爬取成功的資訊也不會顯示出來。
後來,我看了看網頁原始碼:
其中有個“人付款”,這個是中文的呀。
明顯用數字的正則表示式來爬取是不可以爬取到,也許是因為超時了?
反正就是什麼都輸出不來。
成功示例圖:
失敗示例圖:
程式碼,真有意思!