爬取京东商品评论

来源:互联网 发布:51单片机有趣的小制作 编辑:程序博客网 时间:2024/05/17 08:50

大概又是个陈年旧代码放上来看看

代码github:https://github.com/imcy/doubanAnaly
包括爬豆瓣和爬京东以及主题分析


  1. txt文档是用来写入好评和差评的
  2. ’fetchJSON_comment98vv13933’ 这个可以在打开商品页面拉到评论地方后,Chrome浏览器右键检查network,点击更多评论然后会捕捉到

https://club.jd.com/comment/productPageComments.action
这样的一个url,如下图所示,然后就可以看见这个url中的参数,把FetchJSON_comment98vv…复制下来改一下,还有就是Id也改一下,就可以愉快地爬所有评论了
这里写图片描述

# -*- coding: utf-8 -*-import re,requests,jsonimport codecsfrom bs4 import BeautifulSoupfile1=codecs.open('scorePos.txt','a', encoding='utf-8')file2=codecs.open('scoreNeg.txt','a', encoding='utf-8')s=requests.session()url='https://club.jd.com/comment/productPageComments.action'data={    'callback':'fetchJSON_comment98vv13933',    'productId':'5001209',    'score':0,    'sortType':5,    'page':0,    'pageSize':10,    'isShadowSku':0,    'fold':1}while True:    t=s.get(url,params=data).text    try:        t=re.search(r'(?<=fetchJSON_comment98vv13933\().*(?=\);)',t).group(0)    except Exception as e:        break    j=json.loads(t)    commentSummary=j['comments']    for comment in commentSummary:        c_content=comment['content']  # 评论        c_time=comment['referenceTime']        c_name=comment['nickname']        c_client=comment['userClientShow']        score=comment['score']        print(score)        print('{} {} {}\n{}\n'.format(c_name,c_time,c_client,c_content))        if score>=4:            file1.write(c_content+'\n')        if score<=3:            file2.write(c_content+'\n')    data['page']+=1file1.close()file2.close()
原创粉丝点击