使用python进行美团店铺评论的爬取,给出代码
时间: 2024-06-10 07:07:10 浏览: 91
Python实现爬取并分析电商评论
以下是使用Python进行美团店铺评论爬取的代码示例:
```
import requests
from bs4 import BeautifulSoup
url = "https://www.meituan.com/meishi/101572"
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)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
comment_list = soup.find_all("div", class_="comment-item")
for comment in comment_list:
user = comment.find("div", class_="user")
user_name = user.find("span", class_="name").text
comment_text = comment.find("div", class_="comment").text.strip()
print("用户:", user_name)
print("评论:", comment_text)
else:
print("请求失败!")
```
请注意,此代码仅供学习参考,爬取网站的数据要遵守相关法律法规,必须获得合法授权。
阅读全文