python小项目–利用selenium来获取斗鱼直播标签
详细说明代码已经给出
import time
from selenium import webdriver
class Douyu(object):
def __init__(self):
self.url = 'https://www.douyu.com/directory/all'
self.driver = webdriver.Edge()
self.classList = [] #用于存储查找到的集合
def __getdata__(self): # 得到数据
self.classList = self.driver.find_elements_by_xpath('/html/body/section/main/section/div[2]/ul/li/div/a/div[2]')
while True:
# 翻页后直接相加
self.classList = self.classList + self.driver.find_elements_by_xpath('/html/body/section/main/section/div[2]/ul/li/div/a/div[2]')
try:
driver.execute_script('scrollTo(0,1000000)')
el_next = driver.find_elements_by_xpath('/html/body/section/main/section[2]/div[2]/div/ul/li[9]/span')
el_next.click()
except:
break
def __parse_data__(self): # 展示
for data in self.classList:
print(data.text)
def __run__(self):
# 初始化 url、driver实例
# #driver.get
self.driver.get(self.url)
# 定位
self.__getdata__()
# 获取打印
self.__parse_data__()
# 关闭
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
douyu = Douyu()
douyu.__run__()