首先,需要安装好 Python 和相关的库,如 requests、lxml 等。然后,可以按照以下步骤进行编写 XPath 代码:

  1. 发送请求,获取网页内容
import requests

url = 'https://www.douban.com/group/sycq/discussion?start=0'
response = requests.get(url)
html = response.text
  1. 解析网页内容,提取需要的信息
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() 函数将 titleslinks 合并为一个可迭代对象,每次循环同时获取一个帖子的标题和链接。
  • print(title, link):打印每个帖子的标题和链接。
Python 爬取三亚学院豆瓣小组帖子:使用 XPath 代码提取标题和链接

原文地址: https://www.cveoy.top/t/topic/nuEk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录