请给我一段用python写的爬取这两个页面中评论信息的代码,并告诉我如果多几个页面怎么修改代码,第一个网址如下:https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clientVersion=1.0.0&t=1685972010718&loginType=3&uuid=122270672.16658844629262105565150.1665884462.1685254518.1685970174.9&productId=6044018&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield=第二个网址是:https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clientVersion=1.0.0&t=1685972010718&loginType=3&uuid=122270672.16658844629262105565150.1665884462.1685254518.1685970174.9&productId=6044018&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield=
时间: 2023-07-23 20:27:26 浏览: 107
简单的python代码爬取绅士福利!初学也能看懂!
5星 · 资源好评率100%
好的,我来给您提供一段可以爬取多个页面评论信息的代码:
```python
import requests
import json
def crawl_comments(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
response = requests.get(url, headers=headers)
data = json.loads(response.text)
comments = data["comments"]
for comment in comments:
print(comment["content"])
for i in range(1, 11):
url1 = f"https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clientVersion=1.0.0&t=1685972010718&loginType=3&uuid=122270672.16658844629262105565150.1665884462.1685254518.1685970174.9&productId=6044018&score=0&sortType=5&page={i}&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield="
crawl_comments(url1)
for j in range(1, 11):
url2 = f"https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clientVersion=1.0.0&t=1685972010718&loginType=3&uuid=122270672.16658844629262105565150.1665884462.1685254518.1685970174.9&productId=6044019&score=0&sortType=5&page={j}&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield="
crawl_comments(url2)
```
这段代码可以爬取两个页面中的评论信息,并输出评论内容。如果要爬取更多页面,只需要在循环中修改 range 函数的参数即可。
阅读全文