1. 程式人生 > >利用Python打造了淘寶商品資訊定向爬蟲!老闆給五千!我該收嗎?

利用Python打造了淘寶商品資訊定向爬蟲!老闆給五千!我該收嗎?

利用Python打造了淘寶商品資訊定向爬蟲!老闆給五千!我該收嗎?

 

利用Python打造了淘寶商品資訊定向爬蟲!老闆給五千!我該收嗎?

 

程式的結構設計

進群:960410445  即可獲取數十套PDF!

步驟1:提交商品搜尋請求,迴圈獲取頁面

步驟2:對於每個頁面,提取商品名稱和價格資訊

步驟3:將資訊輸出到螢幕上

eval():eval函式能夠將我們獲取的字串最外層的雙引號或者單引號去掉

>>> a=1
>>> eval("a+1")
2
>>> print("a+1")
a+1
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)
 tlt=re.findall(r'"raw_title":".*?"',html)
 for i in range(len(plt)):
 price=eval(plt[i].split(':')[1])
 title=eval(tlt[i].split(':')[1])
 ilt.append([price,title])
 except:
 print("")
 
def printGoodsList(ilt):
 tplt="{:4}	{:8}	{:16}"
 print(tplt.format("序號","價格","商品名稱"))
 count=0
 for g in ilt:
 count=count+1
 print(tplt.format(count,g[0],g[1]))
def main():
 goods='裙子'
 depth=2
 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()

利用Python打造了淘寶商品資訊定向爬蟲!老闆給五千!我該收嗎?

 

私信小編 007 有驚喜哦!