老师你好,我按照你的代码在python上跑,发现跑出不来,请你帮我看看好吗,我是python 3.8:
def spider(sn, book_list=[]):
""" 爬取京东商城的图书信息 “”"
url = ‘https://search.jd.com/Search?keyword={0}’.format(sn)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
# 获取HTML信息
html_data = requests.get(url, headers=headers).text
# print(html_data)
# 获取xpath对象
selector = html.fromstring(html_data)
# 寻找书本列表
ul_list = selector.xpath('//div[@id="J_goodsList"]/ul/li')
print(len(ul_list))
# 解析对应的内容,标题,价格,链接
for li in ul_list:
# 标题
title = li.xpath('div/div[@class="p-name"]/a/@title')
print(title[0])
# 购买链接
link = li.xpath('div/div[@class="p-name"]/a/@href')
print(link[0])
# 价格
price = li.xpath('div/div[@class="p-price"]/strong/i/text()')
print(price[0])
# 店铺
store = li.xpath('div//a[@class="curr-shop"]/@title')
print(store[0])
book_list.append({
'title': title[0],
'price': price[0],
'link': link[0],
'store': store[0]
})
if name == ‘main’:
sn = '9787115428028’
spider(sn)
报错信息如下:
Traceback (most recent call last):
File “C:/Users/jiami/PycharmProjects/book/spider_jd.py”, line 48, in
spider(sn)
File “C:/Users/jiami/PycharmProjects/book/spider_jd.py”, line 36, in spider
print(store[0])
IndexError: list index out of range
谢谢老师哈
登录后可查看更多问答,登录/注册