代码如下
可以自己更改爬取的信息
import requests
from lxml import etree
url = 'https://news.163.com'
resp = requests.get(url)
if resp.status_code != 200:
raise Exception("请求失败")
html_content = resp.text
pattern1 = '//div[@class="mod_top_news2" and @id="js_top_news"]/h2/a/text()'
pattern2 = '//div[@class="mod_top_news2" and @id="js_top_news"]/ul[@class="top_news_ul"]/li/a/text()'
tree = etree.HTML(html_content)
new1 = tree.xpath(pattern1)
new2 = tree.xpath(pattern2)
for i in new1: print(i)
for j in new2: print(j)