這篇文章主要介紹scrapy怎么追蹤python爬蟲的商品評(píng)價(jià),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)是專業(yè)的謝家集網(wǎng)站建設(shè)公司,謝家集接單;提供成都網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行謝家集網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
創(chuàng)建一個(gè)測試的spider
scrapy genspider jdcomment01spider club.jd.com scrapy list --查看一下
1.一些缺的數(shù)據(jù)信息探索
--人名
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", '')
--獲取所有評(píng)價(jià)
一個(gè)商品的總的評(píng)價(jià)信息可以從這個(gè)URL獲取
https://club.jd.com/ProductPageService.aspx?method=GetCommentSummaryBySkuId&referenceId=1601991
返回的是個(gè)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}
具體有多少評(píng)論頁 = CommentCount/30
其他的如Score1Count一星評(píng)論的有多少,AverageScore平均得分都很有用,下次再處理。
2.獲取所有評(píng)論數(shù)
在第一部分的基礎(chǔ)上修改讀取多少也即可,修改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
檢查結(jié)果
scrapy crawl jdcomment01spider -o items.json -t csv
以上是“scrapy怎么追蹤python爬蟲的商品評(píng)價(jià)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!