python爬取抖音评论_使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)
2024-12-26 13:37
好的,以下是爬取豆瓣歌曲评论的Python代码示例:
首先,我们需要导入需要的库:requests、BeautifulSoup和re。
```python
import requests
from bs4 import BeautifulSoup
import re
```
然后,我们需要定义一个函数来获取评论。这个函数将获取豆瓣歌曲页面上的所有评论,并返回一个评论列表。
```python
def get_comments():
url = 'https://music.douban.com/subject/34926236/comments'
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')
comments = []
pattern = re.compile('<p class="">(.*?)</p>', re.S)
results = re.findall(pattern, str(soup))
for result in results:
comment = result.strip()
comments.append(comment)
return comments
```
接下来,我们可以调用这个函数,并打印出所有的评论。
```python
comments = get_comments()
for comment in comments:
print(comment)
```