请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

京东网a标签只能读取其属性,不能读取其文本内容

京东网爬取不到 评论数量

我已经确定找到了 评论数量的那个 a标签对象, 也能读取 那个a标签的属性,比如 评论地址,,但就是不能读取 a标签对象的 文本,是不是跟 编码 有关系?? 但其它a标签文本都能读取啊,想不明白。

图片描述
图片描述

正在回答

插入代码

3回答

之所以获取不到评论,是因为京东的反爬机制,第一次加载的HTML中是没有评论数据的,如下图:

https://img1.sycdn.imooc.com/szimg/5e6e1501097ab60219130930.jpg

评论数据是通过jsonp的方式异步加载过来的,如下图:

https://img1.sycdn.imooc.com//szimg/5e6e14d10981bb4019170922.jpg


0 回复 有任何疑惑可以回复我~
  • 提问者 py_builder #1
    谢谢老师,我按老师的思路已经解决了问题。并且把异同步任务加载概念学习了一遍。
    回复 有任何疑惑可以回复我~ 2020-03-15 21:53:09
  • 提问者 py_builder #2
    #comment_data
        li_ids=[]
        for li in ul:
            li_id = li.xpath('@data-sku')[0]
            li_ids.append(li_id)
        id_code=','.join(li_ids)
        url = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds={0}'.format(id_code)
        js_data = requests.get(url).json()
        comments = js_data['CommentsCount']
        # reuse variables
        li_ids = []
        for comment in comments:
            comment_count = comment['CommentCount']
            # goodcomment_count = comment['GoodCount']
            li_ids.append(comment_count)
    
    先把所有评论数跑一遍储存起来,然后再在后面添加,加快爬虫速度,但写得还是太丑了。。。
    回复 有任何疑惑可以回复我~ 2020-03-15 21:55:46
提问者 py_builder 2020-03-13 09:21:51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def spider_jd(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'
    }
    respond=requests.get(url, headers=headers)
    respond.encoding= 'utf-8'
    html_doc =respond.text
    selector=html.fromstring(html_doc)
    ul=selector.xpath('//div[@id="J_goodsList"]/ul/li')
    count=0
    for li in ul:
        count+=1
        if count == 1:
            print('-' * 100)
            print('-' * 100)
        else:
            print('-' * 100)
        # commerce
        shop = li.xpath('div/div[@class="p-icons"]/i[@data-tips="京东自营,品质保障"]/text()')
        commerce=li.xpath('div/div[@class="p-shopnum"]/a/text()')[0]
        print('商家:{0}'.format(commerce)) if not shop else print('商家:京东自营店')
        #name
        name = li.xpath('div/div/a/@title')[0]
        # title= li.xpath('string(//div/div/a)')
        print('书名:{0}'.format(name))
        #price
        price=li.xpath('div/div[@class="p-price"]/strong/i/text()')[0]
        print('价格:{0}元'.format(price))
        #comment
        comment = li.xpath('string(//div/div[@class="p-commit"]/strong/a)')
        commentlink = li.xpath('div/div[@class="p-commit"]/strong/a/@href')[0]
        print('评论数量{0}'.format(comment))
        # comment = li.xpath('div/div[@class="p-commit"]/strong/a)
        # 对象是存在的,也能读取其中的属性,就是读取不了其文本
        print('评论链接:{0}'.format(commentlink.replace('//', '')))
        # print('{0}条评论'.format(comment))
        #link
        link = li.xpath('div/div[@class="p-name"]/a/@href')[0]
        print('商品链接:{0}'.format(link.replace('//', '')))
        #separate
        print('-'*100)
        book_list.append({
            '商家''当当自营店' if not shop else commerce,
            '书名': name,
            '价格'float(price),
            '评论': commentlink,
            '链接': link
 
        })


0 回复 有任何疑惑可以回复我~
NavCat 2020-03-13 00:05:10

把你的代码,贴出来,我帮你调试下:

https://img1.sycdn.imooc.com//szimg/5e6a5cd20939a8a608240394.jpg

在这里贴

0 回复 有任何疑惑可以回复我~
  • 提问者 py_builder #1
    老师我已经贴出来了,而且我在爬一号店的时候发现,一号店的评论数量是a标签的内容,但a标签里面嵌套了i标签,这样使用text()的时候会生成2个元素的列表,而这个是a标签里面 直接 加了个文本“万条评论”,然后就无法读取了。
    回复 有任何疑惑可以回复我~ 2020-03-13 09:27:02
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号