Python全系列 教程
3567个小节阅读:5931.6k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from lxml import etree
def spider_huya():
# 创建一个驱动
service = Service('./chromedriver.exe')
# 创建一个浏览器
driver = Chrome(service=service)
# 设置隐式等待
driver.implicitly_wait(5)
# 访问网址
driver.get('https://www.huya.com/g/lol')
count = 1
while True:
# print('获取了第%d页' % count)
# count += 1
# 提取数据
e = etree.HTML(driver.page_source)
names = e.xpath('//i[@class="nick"]/@title')
person_nums = e.xpath('//i[@class="js-num"]/text()')
# 打印数据
# for n,p in zip(names,person_nums):
# print(f'主播名:{n} 人气:{p}')
# 找到下一页的按钮
# try:
# next_btn = driver.find_element(By.XPATH,'//a[@class="laypage_next"]')
# next_btn.click()
# except Exception as e:
# break
if driver.page_source.find('laypage_next') == -1:
break
next_btn = driver.find_element(By.XPATH,'//a[@class="laypage_next"]')
next_btn.click()
# 关闭浏览器
driver.quit()
if __name__ == '__main__':
spider_huya()