Python 爬取三亚学院豆瓣小组帖子:使用 XPath 代码提取标题和链接
首先,需要安装好 Python 和相关的库,如 requests、lxml 等。然后,可以按照以下步骤进行编写 XPath 代码:
- 发送请求,获取网页内容
import requests
url = 'https://www.douban.com/group/sycq/discussion?start=0'
response = requests.get(url)
html = response.text
- 解析网页内容,提取需要的信息
from lxml import etree
# 将获取到的网页内容转换为HTML格式,并进行解析
selector = etree.HTML(html)
# 使用xpath表达式提取需要的信息
# 这里以获取帖子标题和链接为例
titles = selector.xpath('//table[@class='olt']/tr/td[@class='title']/a/@title')
links = selector.xpath('//table[@class='olt']/tr/td[@class='title']/a/@href')
# 打印结果
for title, link in zip(titles, links):
print(title, link)
注释:
etree.HTML(html):将获取到的网页内容转换为 HTML 格式,并进行解析。selector.xpath():使用 XPath 表达式提取需要的信息。//table[@class='olt']/tr/td[@class='title']/a/@title:获取帖子标题,其中//表示在整个 HTML 文档中查找,[@class='olt']表示找到class属性值为 'olt' 的table标签,/tr表示找到table标签下的tr标签,/td[@class='title']表示找到tr标签下的class属性值为 'title' 的td标签,/a/@title表示找到td标签下的a标签的title属性值。//table[@class='olt']/tr/td[@class='title']/a/@href:获取帖子链接,其中//和[@class='olt']/tr/td[@class='title']/a/的含义同上,@href表示获取a标签的href属性值。for title, link in zip(titles, links)::使用zip()函数将titles和links合并为一个可迭代对象,每次循环同时获取一个帖子的标题和链接。print(title, link):打印每个帖子的标题和链接。
原文地址: https://www.cveoy.top/t/topic/nuEk 著作权归作者所有。请勿转载和采集!