表情党网址:https://qq.yh31.com/
目标:抓取表情党主页的热门栏目名称及对应的链接,如下图所示:
按F12(谷歌浏览器),进入开发者工具模式,进行页面分析,在Elements板块下,进入搜索栏(Ctrl+F),在框中输入“//div[@id=“dibu_cc”]//li/a“,可以匹配到所有的热门栏目a标签,如下图:
源码如下:
import requests
from lxml import etree
url = 'https://qq.yh31.com'
headers= {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
res = requests.get(url, headers=headers)
tree = etree.HTML(res.content)
rm_lst = tree.xpath('//div[@id="dibu_cc"]//li/a')
for rm in rm_lst:
print('热门栏目名称:', end=' ')
print(rm.xpath('./text()'))
print('热门栏目链接:', end=' ')
print(url + rm.xpath('./@href')[0])
运行结果如下: