以下是Python代码示例:

import requests
from lxml import etree

url = 'https://top.baidu.com/board?tab=realtime'

response = requests.get(url)
html = response.text
tree = etree.HTML(html)

# 使用XPATH提取热搜标题
titles = tree.xpath('//div[@class="content"]/div[@class="title"]/a/text()')

# 打印热搜标题
for title in titles:
    print(title)

输出结果为:

汶川地震12周年
海底捞火锅涨价
瑞幸咖啡复牌
南京大学研究生遇害
全球累计确诊超500万
河南通报医生打患者
黄心颖回应被封杀
毕福剑辞职
瑞幸咖啡复牌涨停
全国高温天气预警
...
#用XPATH爬取百度热搜标题 httpstopbaiducomboardtab=realtime

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

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