请告诉我用python如何爬取这两个链接的京东评论并且将结果按句写到txt文件里,第一个网址是“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=1686056128680&loginType=3&uuid=122270672.16658844629262105565150.1665884462.1685970174.1686056049.10&productId=6044018&score=0&sortType=5&page=7&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield=”
时间: 2023-08-26 08:12:54 浏览: 172
好的,我可以为您提供一些帮助。
首先,您需要使用requests库来向这两个链接发送GET请求。您可以通过以下方式获取评论数据:
```python
import requests
import json
url1 = "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="
url2 = "https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clientVersion=1.0.0&t=1686056128680&loginType=3&uuid=122270672.16658844629262105565150.1665884462.1685970174.1686056049.10&productId=6044018&score=0&sortType=5&page=7&pageSize=10&isShadowSku=0&rid=0&fold=1&bbtf=&shield="
response1 = requests.get(url1)
response2 = requests.get(url2)
data1 = json.loads(response1.text)
data2 = json.loads(response2.text)
comments1 = data1['comments']
comments2 = data2['comments']
```
这将获取两个链接中的评论数据并将其存储在变量“comments1”和“comments2”中。
接下来,您需要将评论数据按照句子进行拆分并写入txt文件中。您可以使用nltk库来拆分句子并使用以下代码将其写入txt文件中:
```python
import nltk
def write_comments_to_file(comments, file):
with open(file, 'w', encoding='utf-8') as f:
for comment in comments:
sentences = nltk.sent_tokenize(comment['content'])
for sentence in sentences:
f.write(sentence + '\n')
write_comments_to_file(comments1, 'comments1.txt')
write_comments_to_file(comments2, 'comments2.txt')
```
这将拆分每个评论中的句子,并将其写入名为“comments1.txt”和“comments2.txt”的txt文件中。
希望这可以帮助您开始爬取京东评论!
阅读全文