from selenium import webdriver
import time
class DouyuSpider:
def __init__(self):
self.start_url = "https://www.douyu.com/directory/all"
self.driver = webdriver.Chrome()
def get_content_list(self):
li_list = self.driver.find_elements_by_xpath("//div[@class='layout-Module-container layout-Cover ListContent']/ul/li")
content_list = []
for li in li_list:
item = {}
item["room_title"] = li.find_element_by_xpath(".//h3").text
item["room_img"] = li.find_element_by_xpath(".//img[@class='DyImg-content is-normal']").get_attribute("src")
item["anchor_name"] = li.find_element_by_xpath(".//h2").text
item["watch_num"] = li.find_element_by_xpath(".//span[@class='DyListCover-hot']").text
item["room_cate"] = li.find_element_by_xpath(".//span[@class='DyListCover-zone']").text
print(item)
def run(self):
# 1.start_url
# 2.发送请求,获取响应
self.driver.get(self.start_url)
# 3.提取数据,提取下一页页的元素
self.get_content_list()
if __name__ == '__main__':
douyuspider = DouyuSpider()
douyuspider.run()