温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

scrapy怎么追踪python爬虫的商品评价

发布时间:2020-11-23 15:53:47 来源:亿速云 阅读:128 作者:小新 栏目:编程语言

这篇文章主要介绍scrapy怎么追踪python爬虫的商品评价,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创建一个测试的spider

scrapy genspider jdcomment01spider club.jd.com
scrapy list --查看一下

1.一些缺的数据信息探索

--人名

comment0 = response.xpath('//div[@id="comment-0"]')
print comment0.xpath('.//div[@class="item"]//div[@class="user"]//div[@class="u-name"]/text()').extract_first().replace("\r\n", '')

--获取所有评价

一个商品的总的评价信息可以从这个URL获取

https://club.jd.com/ProductPageService.aspx?method=GetCommentSummaryBySkuId&referenceId=1601991

 

返回的是个JSON字符串

{"SkuId":1601991,"ProductId":1601991,"Score1Count":115,"Score2Count":24,"Score3Count":77,"Score4Count":229,"Score5Count":3250,"ShowCount":311,"CommentCount":3695,"AverageScore":5,"GoodCount":3479,"GoodRate":0.942,"GoodRateShow":94,"GoodRateStyle":141,"GeneralCount":101,"GeneralRate":0.027,"GeneralRateShow":3,"GeneralRateStyle":4,"PoorCount":115,"PoorRate":0.031,"PoorRateShow":3,"PoorRateStyle":5}

具体有多少评论页 = CommentCount/30

 其他的如Score1Count一星评论的有多少,AverageScore平均得分都很有用,下次再处理。

2.获取所有评论数

在第一部分的基础上修改读取多少也即可,修改jdcomment01spider.py,代码如下

# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import Spider
from scrapy.selector import Selector
from tutorial.items import DmozItem
import urllib2
import math
import json
itemnum = '1601991'
commentpeypage = 30
class Jdcomment01spiderSpider(scrapy.Spider):
    name = "jdcomment01spider"
    allowed_domains = ["club.jd.com"]
    itemsummaryurl='http://club.jd.com/ProductPageService.aspx?method=GetCommentSummaryBySkuId&referenceId=' + itemnum
    itemsummaryresponse = urllib2.urlopen(url)
    itemsummaryjson_dict = json.loads(itemsummaryresponse.read())
    commentrange = int(math.ceil(itemsummaryjson_dict.get('CommentCount'))/commentpeypage)
 
    start_urls = []
    for i in range(commentrange):
        s_url = "http://club.jd.com/review/" + itemnum + "-" + str(i) + "-0.html/",
        start_urls.append(s_url)
 
    def parse(self, response):
        sel = Selector(response)
        sites = sel.xpath('//ul/li')
        items = []
        for i in range(0, commentpeypage):
            divs = response.xpath('//div[@id="' + str(i) + '"]')
            uid = divs.xpath('.//div[@class="item"]//div[@class="user"]//div[@class="u-name"]/text()').extract_first().replace("\r\n", '')
            for zz in divs.xpath('.//dl'):
                item = DmozItem()
                item['prodid'] = itemnum
                item['userid'] = 'userid'
                item['type'] = zz.xpath('.//dt/text()').extract_first().replace("\r\n", '')
                item['desc'] = zz.xpath('.//dd/text()').extract_first().replace("\r\n", '')
                items.append(item)
        return item

检查结果

scrapy crawl jdcomment01spider -o items.json -t csv

以上是“scrapy怎么追踪python爬虫的商品评价”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI