[python]代码库
# coding:utf-8
import requests
import codecs
import json
# 爬取京东商品信息
def getCommentCounts(productId):
url = 'https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv33&productId=' + productId + '&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
# 模拟浏览器
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}
# 请求
r = requests.get(url, headers=header)
# 将json数据按照特殊的编码格式decode()
html = r.content.decode()
# 去除收尾
index = html.find('{')
html = html[index:-2]
# 将json字符串转为字典
data = json.loads(html)
# 获取评论总数
commentCounts = data['productCommentSummary']['commentCount']
print('评论数:', commentCounts)
if __name__ == '__main__':
productId = '7774757'
getCommentCounts(productId)