栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

scrapy如何追踪python爬虫的商品评价?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

scrapy如何追踪python爬虫的商品评价?

在我们进行购物的时候,同样先看到了的都是产品介绍,不外乎是好用值得购买之类的,有些还请了人做宣传,仅从商品信息上还不足以让我们下定决心去购买这件商品。我们需要思考一个问题,这件东西真的像它说的那么好吗?这时候我们就要知道商品的评价。所以今天小编就教大家一个用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("rn", '')

--获取所有评价

一个商品的总的评价信息可以从这个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("rn", '')
            for zz in divs.xpath('.//dl'):
                item = DmozItem()
                item['prodid'] = itemnum
                item['userid'] = 'userid'
                item['type'] = zz.xpath('.//dt/text()').extract_first().replace("rn", '')
                item['desc'] = zz.xpath('.//dd/text()').extract_first().replace("rn", '')
                items.append(item)
        return item

检查结果

scrapy crawl jdcomment01spider -o items.json -t csv

 

根据运行后的结果图片来看,我们已经成功获取了那些商品的评价了 ,接下来就可以看一下商品的购买价值,也是一种避免踩雷的好办法,小伙伴们赶紧试试是不是这么好用~更多Python学习推荐:PyThon学习网教学中心。


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/11870.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号