前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python爬虫(二十三)_selenium案例:动态模拟页面点击

Python爬虫(二十三)_selenium案例:动态模拟页面点击

作者头像
用户1174963
发布2018-01-17 12:34:25
1K0
发布2018-01-17 12:34:25
举报
文章被收录于专栏:python学习指南python学习指南

本篇主要介绍使用selenium模拟点击下一页,更多内容请参考:Python学习指南

代码语言:javascript
复制
#-*- coding:utf-8 -*-

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time

class douyuSelenium(unittest.TestCase):
    #初始化方法
    def setUp(self):
        self.driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])

    #具体的测试用例方法,一定要以test开头
    def testDouyu(self):
        self.driver.get("http://www.douyu.com/directory/all")

        while True:
            #指定xml解析
            soup = BeautifulSoup(self.driver.page_source, "lxml")

            #返回当前页面的所有房间标题列表和观众人数列表
            titles = soup.find_all('h3' , {'class':"ellipsis"})
            print(len(titles))
            nums = soup.find_all('span', {'class': "dy-num fr"})

                # #使用zip()函数把列表合并,并创建一个远相对的列表[(1, 2), (3, 4)]
            for title, num in zip(nums, titles):
                print(u'观众人数: '+num.get_text().strip(), u'\t房间标题: '+title.get_text().strip())
            #page_source.find()未找到内容则返回-1
            if self.driver.page_source.find('shark-pager-disable-next') != -1:
                break
            self.driver.find_element_by_class_name('shark-pager-next').click()
            time.sleep(1)

    # 退出时的清理方法
    def tearDown(self):
        print("加载完成...")
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-12-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档