京东商品评价爬虫

京东商品评价爬虫

最近因为一些事情,需要爬一下京东商品的评论(大部分是书籍)

  • 准备环境:PyCharm,python3.5.2

话不多说赶紧上代码:

# -*- coding: utf-8 -*-import re, json, requests
import codecs
from bs4 import BeautifulSoup
import csv
import oss = requests.session()
url = 'https://club.jd.com/comment/productPageComments.action'
data = {'callback': 'fetchJSON_comment98vv13933',# 需要抓取评论的商品id'productId': '11936238',# score 参数说明:# 0  抓取所有评论(好评在前)# 1  抓取所有差评# 2  抓取所有中评# 3  抓取所有追评# 4  抓取所有配图评论'score': 1,'sortType': 5,'page': 0,'pageSize': 10,'isShadowSku': 0,'fold': 1
}# 设置抓取目标评论数
target_cnt = 100# 设置保存文件名
target_file = str(data['productId']) + '_' + str(data['score']) + '.csv'cnt = 1with open(target_file, "w", encoding='utf8', newline='') as csvFile:writer = csv.writer(csvFile, quoting=csv.QUOTE_ALL)writer.writerow(["stars", "time", "comment"])while cnt <= target_cnt:t = s.get(url, params=data).texttry:t = re.search(r'(?<=fetchJSON_comment98vv13933\().*(?=\);)', t).group(0)except Exception as e:breakj = 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))writer.writerow([score, c_time, c_content])data['page'] += 1cnt = cnt + 1csvFile.close()

大概没什么其他需要讲的了吧,当然这个爬虫是在别的地方找的。而且也是最简单的一类,没有做反反爬处理。这些以后会找机会记录。

来源:Github

转载于:https://www.cnblogs.com/georgeyang/p/9077118.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部