腾讯新闻爬取
from selenium import webdriver
from lxml import etree
import time
url = 'https://news.qq.com/'
bro = webdriver.Chrome()
bro.get(url)
for i in range(5):
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(3)
res = bro.page_source
tree = etree.HTML(res)
lis = tree.xpath('//ul[@class="list"]/li')
for li in lis:
try:
title = li.xpath('./div[@class="detail"]/h3/a/text()')[0]
url = li.xpath('./div[@class="detail"]/h3/a/@href')[0]
print(title,url)
except:
continue
结果:


本文介绍了一种使用Selenium和LXML抓取腾讯新闻网站数据的方法。通过模拟浏览器滚动加载更多内容,解析并提取了新闻标题和链接。适用于学习网络爬虫技术和数据抓取。
1万+

被折叠的 条评论
为什么被折叠?



