用python正则表达式爬取百度热搜标题
以下是一个示例代码,使用Python的正则表达式模块re和requests库来爬取百度热搜榜单的标题。
import re
import requests
url = 'https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&word=%E7%83%AD%E6%90%9C'
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.3'}
response = requests.get(url, headers=headers)
html = response.content.decode('utf-8')
pattern = re.compile('<a.*?class="c-title.*?".*?>(.*?)</a>', re.S)
titles = pattern.findall(html)
for title in titles:
print(title)
运行代码后,会输出百度热搜榜单的所有标题。其中,正则表达式<a.*?class="c-title.*?".*?>(.*?)</a>匹配了每一个标题的HTML标签,并且通过findall()方法获取了所有匹配的结果。最后,利用for循环输出所有的标题
原文地址: https://www.cveoy.top/t/topic/e01i 著作权归作者所有。请勿转载和采集!