首页 > 应用开发 > > Python爬虫爬取淘宝商品书包的名称价格
import requests
import re
def gethtmltext(url):
try:
r=requests.get(url)
r.raise_for_status
r.encoding=r.apparent_encoding
return r.text
except:
return""
def getlist(goodlist,html):
goodname=re.findall(r'"raw_title":".?"',html)
goodprice=re.findall(r'"view_price":"[\d.]
"',html)
for i in range(len(goodname)):
price=eval(goodprice[i].split(":")[1])
name=eval(goodname[i].split(":")[1])
goodlist.append([name,price])
def printlist(goodlist):
count=0
plt="{0:^10}\t{1:^10}\t{2:^10}"
print(plt.format("编号","名称","价格"))
for g in goodlist:
count = count + 1
print(plt.format(count, g[0],g[1]))
def main():
url="https://s.taobao.com/search?q="
prl="书包"
glist=[]
html=gethtmltext(url+prl)
getlist(glist,html)
printlist(glist)
main()
此代码没有翻页处理,好像淘宝现在要登录才能爬取,网址变了,但是我不知道怎么改这个代码,求指导
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1556/viewspace-3156602/ ,如需转载,请注明出处,否则将追究法律责任。