爬虫-斗鱼直播

from selenium import webdriver
import time
from lxml import etree

driver = webdriver.PhantomJS(executable_path=r'F:\老师发的文件\系统班第四阶段\day923\phantomjs-2.1.1-windows\bin\phantomjs.exe')
def request_html(url):
    driver.get(url=url)
    time.sleep(3)
    driver.save_screenshot('douyu.png')
    with open('douyu.html','w',encoding='utf-8') as f:
        f.write(driver.page_source)


def parse_html(response):
    tree = etree.HTML(response)
    li_list = tree.xpath('//div[@class="layout-Module-container layout-Cover ListContent"]/ul/li')
    # print(len(li_list))
    for li in li_list:
        type = li.xpath('.//div[@class="DyListCover-content"]/div[1]/span/text()')[0]
        title = li.xpath('.//div[@class="DyListCover-content"]/div[1]/h3/text()')[0]
        hot = li.xpath('.//div[@class="DyListCover-content"]/div[2]/span/text()')[0]
        username = li.xpath('.//div[@class="DyListCover-content"]/div[2]/h2/text()')[0]
        print(type,'--',title,'--',hot,'--',username)
    return tree
if __name__ == '__main__':
    url = 'https://www.douyu.com/directory/all'
    # 建立连接
    request_html(url)
    # 返回结果
    response = driver.page_source
    # 获取数据
    tree = parse_html(response)
    page = 1
    # 点击下一页
    while True:
        print('==============={}==================='.format(page))
        flag = tree.xpath('//li[@title="下一页"]/@aria-disabled')[0]
        if flag == 'true':
            break
        driver.find_element_by_class_name(' dy-Pagination-next').click()
        time.sleep(3)
        response = driver.page_source
        tree = parse_html(response)
        page += 1



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值