基本信息
源码名称:python 斗鱼直播间爬取代码
源码大小:1.38KB
文件格式:.py
开发语言:Python
更新时间:2018-04-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 8 元×
微信扫码支付:8 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
爬取所有斗鱼直播间信息。,模拟chrome浏览器操作
#-*- coding: utf-8 -*- import unittest from selenium import webdriver from bs4 import BeautifulSoup as bs class douyu(unittest.TestCase): #初始化方法 def setUp(self): self.option = webdriver.ChromeOptions() self.option.add_argument("test-type") self.driver = webdriver.Chrome(chrome_options=self.option) self.driver=webdriver.Chrome() self.num=0 # 测试方法必须有test开头 def testDouyu(self): self.driver.get("https://www.douyu.com/directory/all") while True: soup= bs(self.driver.page_source,"lxml") names=soup.find_all("h3",{"class":"ellipsis"}) numbers=soup.find_all("span",{"class":"dy-num fr"}) #zip(names,numbers)将这两个列表合并为一个元组 for name,number in zip(names,numbers): print(u"观众人数:%s"%number.get_text().strip() u"\t房间名:%s"%name.get_text().strip()) self.num =1 if self.driver.page_source.find("shark-pager-disable-next")!=-1: break self.driver.find_element_by_class_name("shark-pager-next").click() #测试结束后执行的方式 def tearDown(self): print("当前直播人数: %d"%self.num) self.driver.quit() if __name__=="__main__": #启动测试模块 unittest.main()