答疑·限时优惠

如果,你想让我看见你的疑问并且百分之百的回答。可以加入我的知识星球。
AI悦创·进化岛
AI悦创·进化岛

你好,我是悦创。

先修知识:str.find():>>> Python find()

"""
project = 'Code', file_name = 'test', author = 'AI悦创'
time = '2020/3/13 19:21', product_name = PyCharm
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
"""
import requests
import json
url = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds=29505771199&callback=jQuery7981360'

html = requests.get(url).text
start_position = html.find('{"CommentsCount":')
end_position = html.find('"PoorRateStyle":0}]}') + len('"PoorRateStyle":0}]}')
result = html[start_position: end_position]
res = json.loads(result)
print(len(res['CommentsCount']))
print(res['CommentsCount'][0]['DefaultGoodCountStr'])
dict_data = res['CommentsCount'][0]
result = dict_data.get('DefaultGoodCountStr', None)
print(result)

运行结果:

1
10+
10+

尝试第二个链接:

"""
project = 'Code', file_name = 'test', author = 'AI悦创'
time = '2020/3/13 19:21', product_name = PyCharm
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
"""
import requests
import json
# url = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds=29505771199&callback=jQuery7981360'
url = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds=12353915&callback=jQuery7981360'
html = requests.get(url).text
start_position = html.find('{"CommentsCount":')
end_position = html.find('"PoorRateStyle":0}]}') + len('"PoorRateStyle":0}]}')
result = html[start_position: end_position]
res = json.loads(result)
print(len(res['CommentsCount']))
print(res['CommentsCount'][0]['DefaultGoodCountStr'])
dict_data = res['CommentsCount'][0]
result = dict_data.get('DefaultGoodCountStr', None)
print(result)

运行结果:

1
12万+
12万+

多个商品 id

import requests
import json
# 多个商品 id
url = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds=12353915,11993134&callback=jQuery7981360'
html = requests.get(url).text
start_position = html.find('{"CommentsCount":')
end_position = html.find('"PoorRateStyle":0}]}') + len('"PoorRateStyle":0}]}')
result = html[start_position: end_position]
res = json.loads(result)
# 第一个 id
print('第一个 id')
print(len(res['CommentsCount']))
print(res['CommentsCount'][0]['DefaultGoodCountStr'])
dict_data = res['CommentsCount'][0]
result = dict_data.get('DefaultGoodCountStr', None)
print(result)
# 第二个 id
print('第二个 id')
print(len(res['CommentsCount']))
print(res['CommentsCount'][1]['DefaultGoodCountStr'])
dict_data = res['CommentsCount'][1]
result = dict_data.get('DefaultGoodCountStr', None)
print(result)

运行结果:

第一个 id
2
12万+
12万+
第二个 id
2
23万+
23万+
AI悦创·创造不同!
AI悦创 » 手札-11(京东实战手札)

Leave a Reply