以下是使用Python脚本采集https://www.court.gov.cn/zixun.html地址,解析xpath并对title进行解码的示例代码:

import requests
from lxml import etree

url = 'https://www.court.gov.cn/zixun.html'
response = requests.get(url)
html = response.content.decode('utf-8')
selector = etree.HTML(html)
news_list = selector.xpath('/html/body/div[6]/div[2]/div[1]/div[1]/div[2]/ul/li')

for news in news_list:
    title = news.xpath('./a/text()')[0].encode('iso-8859-1').decode('utf-8')
    print(title)

解释:

  1. 使用requests库发送GET请求获取页面内容,编码方式为utf-8。
  2. 使用lxml库的etree模块解析网页内容,生成XPath选择器。
  3. 使用XPath选择器选取新闻列表,遍历列表。
  4. 对每条新闻,使用XPath选择器选取标题,将其编码方式转为iso-8859-1再解码为utf-8,避免出现乱码。
  5. 打印每条新闻的标题
使用python脚本采集httpswwwcourtgovcnzixunhtml地址xpath为htmlbodydiv6div2div1div1div2ultitle解码

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

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