请告诉我用python如何爬取这两个链接的京东评论并且将结果按句写到txt文件里,并且不使用nltk库,第一个网址是“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-07-23 19:30:27 浏览: 183
利用Python网络爬虫对京东商城中指定商品下的用户评论进行爬取,对数据预处理操作后进行文本情感分析并可视化显示。.zip
5星 · 资源好评率100%
好的,这里给您提供一个示例代码,可以爬取这两个链接的京东评论并将结果按句写入txt文件中:
```python
import requests
import re
def get_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.36 Edge/16.16299'
}
response = requests.get(url, headers=headers)
pattern = re.compile(r'"content":"(.*?)",', re.S)
comments = pattern.findall(response.text)
return comments
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='
comments = []
comments.extend(get_comments(url1))
comments.extend(get_comments(url2))
with open('comments.txt', 'w', encoding='utf-8') as f:
for comment in comments:
f.write(comment + '\n')
```
这段代码使用requests库向京东评论API发送请求,并使用正则表达式提取评论内容。最后将所有评论按句写入txt文件中。
阅读全文