正在学习mooc里的python爬虫课程,在编写课程里的实例遇到了一些问题。
以下代码想要得到淘宝商品搜索页面中宝贝标题和价格的列表,按照视频上老师的代码写的但是没有出结果,希望有大神看到可以帮忙解答一下。
ps:本人是python初学小透明,如果犯了低级错误希望大家不要介意,谢谢
【代码如下】
import requests
import re
def gerHTMLText(url):
try:
r = requests,get(url)
r.raise_for_status()
r.encoding = 'utf-8'
r.headers = 'Mozilla10'
return r.text
except:
print("杩炴帴澶辫触")
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])
tltle = eval(tlt[i].split(':')[1])
ilt.append([price, title])
except:
print("")
def printGoodsList(ilt):
tplt = "{:4}\t{:8}\t{: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()