python爬取淘宝评价
时间: 2024-02-23 10:55:13 浏览: 182
Python可以使用第三方库来实现爬取淘宝评价的功能,其中比较常用的是`requests`和`BeautifulSoup`库。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def get_taobao_reviews(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)
soup = BeautifulSoup(response.text, 'html.parser')
reviews = []
review_items = soup.find_all('div', class_='J_KgRate_ReviewItem')
for item in review_items:
review = item.find('div', class_='tb-r-act-bar').text.strip()
reviews.append(review)
return reviews
# 示例使用
url = 'https://item.taobao.com/item.htm?spm=a230r.1.14.1.6e2b2d2fj7XZ8D&id=123456789'
reviews = get_taobao_reviews(url)
for review in reviews:
print(review)
```
这段代码使用了`requests`库发送HTTP请求,获取淘宝商品页面的HTML内容,然后使用`BeautifulSoup`库解析HTML,提取评价信息。你只需要将`url`参数替换为你要爬取的淘宝商品链接即可。
阅读全文