当当网自营价格抓取不到,如何全部抓取?

来源:2-8 爬取当当网的数据

jingkingW

2019-02-24

自营价格p标签下的class=“price e_price”,商家class="price"
代码如图2。
#价格
price = li.xpath(‘p[@class=“price e_price”]/span[@class=“search_now_price”]/text()’),全部价格均抓取不到
#价格
price = li.xpath(‘p[@class=“price”]/span[@class=“search_now_price”]/text()’),商家价格可抓取,自营价格抓取不到
如何全部都抓取到?当当网自营价格
代码

import requests
from lxml import html
def spider(sn):
""“爬去当当网数据”""
url = ‘http://search.dangdang.com/?key={sn}&act=input’.format(sn=sn)
# 获取HTML内容
html_data = requests.get(url).text
# xpath 对象
selector = html.fromstring(html_data)

#找到书本列表
bk_list = selector.xpath('//div[@id="search_nature_rg"]/ul/li')
print(len(bk_list))
for li in bk_list:
    #标题
    title = li.xpath('a/@title')
    print(title[0])
    #链接
    link = li.xpath('a/@href')
    print(link[0])
    #价格
    price = li.xpath('p[@class="price e_price"]/span[@class="search_now_price"]/text()')
    print(price)
    print('------------------------')

if name == ‘main’:
sn='9787111603702’
spider(sn)

写回答

2回答

NavCat

2019-02-26

price = li.xpath('.//p[starts-with(@class,"price")]/span[@class="search_now_price"]/text()')

可以参考我这个写法

0
0

NavCat

2019-02-25

贴一下你的代码,老师帮你调试一下

0
3
NavCat
回复
jingkingW
爬虫就是一个细致活,再有一点就是别人的网站结构可能会经常变,所以爬虫程序也需要去不断的分析目标网站,做对应的更新。
2019-02-26
共3条回复

手把手教你把Python应用到实际开发 不再空谈语法

学会项目开发思路,掌握Python高阶用法。

1341 学习 · 244 问题

查看课程