爬取 淘宝书包页面,为什么能够打印表头,却无法打印数据
本帖最后由 Fucu 于 2020-3-19 18:26 编辑import re
import requests
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.split(':')) #eval 函数用来去掉双引号,split用来分隔字符串
title = eval(tlt.split(':'))
ilt.append()
except:
print('')
def printGoodsList(ilt):
tplt = '{:4}\t{:8}\t{:16}'
print(tplt.format('序号','价格','商品名称'))
count = 0
for g in ilt:
count = count + 1
print(tple.format(count,g,g))
def main():
goods = '书包'
status_url = 'http://s.taobao.com/search?q=' + goods
depth = 2
infoList = []
for i in range(depth):
try:
url = status_url+'&s='+str(44*i)
html = getHTMLText(url)
parsePage(infoList,html)
except:
continue
printGoodsList(infoList)
main()
?
把代码发上来 import re
import requests
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.split(':')) #eval 函数用来去掉双引号,split用来分隔字符串
title = eval(tlt.split(':'))
ilt.append()
except:
print('')
def printGoodsList(ilt):
tplt = '{:4}\t{:8}\t{:16}'
print(tplt.format('序号','价格','商品名称'))
count = 0
for g in ilt:
count = count + 1
print(tple.format(count,g,g))
def main():
goods = '书包'
status_url = 'http://s.taobao.com/search?q=' + goods
depth = 2
infoList = []
for i in range(depth):
try:
url = status_url+'&s='+str(44*i)
html = getHTMLText(url)
parsePage(infoList,html)
except:
continue
printGoodsList(infoList)
main()
页:
[1]