python 爬取知乎问题的回答

2023-04-14 07:33:10   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《python 爬取知乎问题的回答》,欢迎阅读!
回答,python,问题

python 爬取知乎问题的回答

以下是一个简单的爬虫代码,可以爬取指定知乎问题的回答: ```python import requests

from bs4 import BeautifulSoup #设置请求头,模拟浏览器访问。 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'

}

#需要爬取回答的问题链接。 #发送GET请求,获取HTML页面。

response = requests.get(url, headers=headers) html = response.text

# 使用 BeautifulSoup 解析 HTML 页面。 soup = BeautifulSoup(html, 'html.parser') #找到所有回答的标签,根据需要再进一步解析。

answers = soup.find_all('div', {'class': 'List-item'}


for answer in answers: #找到回答的内容。

content = answer.find('div', {'class': 'RichContent-inner'}.get_text().strip()

author = answer.find('span', {'class': 'UserLink AuthorInfo'}.get_text().strip()

print('Answer:', content) print('Author:', author) print() ```


本文来源:https://www.dywdw.cn/4ce54977084e767f5acfa1c7aa00b52acec79c5b.html

相关推荐
推荐阅读