—注意

①检查是否安装requests库

②import requests

爬京东评论逻辑

1、通过抓包得知京东评论的url:一般为xhr或js

1
https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clientVersion=1.0.0&t=1699439745429&loginType=3&uuid=122270672.16994385407911494698389.1699438541.1699438541.1699438541.1&productId=100062014247&score=0&sortType=5&page=1&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield=

2、分析response的结构

image-20231108184155302

可以发现最外层是一个字典,comments键的值是一个列表,有10个元素,每个元素又是一个字典。

3、代码实现

1
2
3
4
text = response.text                 #回复内容在response.text里面,text是json
loads = json.loads(text) #json转换为字典
data_arr.extend(loads['comments']) #data_arr 保存这个列表
data_arr[0]['content'] #第一个商品的评论