python爬蟲(chóng)爬取淘寶商品信息
本文實(shí)例為大家分享了python爬取淘寶商品的具體代碼,供大家參考,具體內(nèi)容如下
import requests as req
import re
def getHTMLText(url):
try:
r = req.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def parasePage(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}\t{:8}\t{:16}"
print(tplt.format("序列號(hào)", "價(jià)格", "商品名稱(chēng)"))
count = 0
for j in ilt:
count = count + 1
print(tplt.format(count, j[0], j[1]))
def main():
goods = "python爬蟲(chóng)"
depth = 3
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)
parasePage(infoList, html)
except:
continue
printGoodsList(infoList)
main()
效果圖:

更多內(nèi)容請(qǐng)參考專(zhuān)題《python爬取功能匯總》進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的ping工具方法
今天小編就為大家分享一篇python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的ping工具方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python機(jī)器學(xué)習(xí)實(shí)現(xiàn)決策樹(shù)
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)實(shí)現(xiàn)決策樹(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
淺談Pandas dataframe數(shù)據(jù)處理方法的速度比較
這篇文章主要介紹了淺談Pandas dataframe數(shù)據(jù)處理方法的速度比較,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
tensorflow保持每次訓(xùn)練結(jié)果一致的簡(jiǎn)單實(shí)現(xiàn)
今天小編就為大家分享一篇tensorflow保持每次訓(xùn)練結(jié)果一致的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Django事務(wù)transaction的使用以及多個(gè)裝飾器問(wèn)題
這篇文章主要介紹了Django事務(wù)transaction的使用以及多個(gè)裝飾器問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python WebSocket長(zhǎng)連接心跳與短連接的示例
這篇文章主要介紹了Python WebSocket長(zhǎng)連接心跳與短連接的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-11-11

