Python爬虫selenium模块

简介: Python爬虫selenium模块

安装

pip install selenium

官方文档:https://www.seleniumhq.org/docs/


测试页面

http://www.pythonscraping.com/pages/javascript/ajaxDemo.html


报错:warnings.warn(‘Selenium support for PhantomJS has been deprecated, please use headless ’

解决: 安装版本2

pip install "selenium < 3"

简单示例解析javascript

from selenium import webdriver
from bs4 import BeautifulSoup
import time
# 显示等待页面加载
def getPage1():
    url = "http://www.pythonscraping.com/pages/javascript/ajaxDemo.html"
    driver = webdriver.PhantomJS()
    driver.get(url)
    time.sleep(1)  # 设置等待时间
    # 获取内容
    content = driver.find_element_by_id("content")
    print(content.text)
    # 通过bs解析
    html = driver.page_source  # 源代码字符串
    soup = BeautifulSoup(html, "html.parser")
    tag = soup.find(id="content")
    print(tag)
    driver.close()
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 隐式等待页面加载
def getPage2():
    url = "http://www.pythonscraping.com/pages/javascript/ajaxDemo.html"
    driver = webdriver.PhantomJS(executable_path="phantomjs")
    driver.get(url)
    # 等待页面加载完毕,获取明显元素作为标志
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "loadedButton"))
        )
    finally:
        # 获取内容
        content = driver.find_element_by_id("content")
        print(content.text)
        driver.close()
getPage2()

点击百度自动提交

# -*- coding:utf-8 -*-
from selenium import webdriver
import time
def clickBaidu():
    # 为了看到效果,使用chrome浏览器
    driver = webdriver.Chrome()
    time.sleep(5)
    baidu = "http:www.baidu.com"
    driver.get(baidu)
    time.sleep(5)
    driver.find_element_by_id("kw").send_keys("百度")
    time.sleep(5)
    driver.find_element_by_id("su").click()
    time.sleep(5)
    # 截屏
    driver.get_screenshot_as_file("baidu_shot.png")
    time.sleep(5)
    driver.close()
clickBaidu()
# 说明,time.sleep() 是为了看到浏览器整个自动过程,实际使用可以去掉

鼠标动作

element.click()
element.click_and_hold()
element.release()
element.double_click()

参考:

python selenium启动浏览器打开百度搜索

相关文章
|
1月前
|
数据采集 测试技术 API
python爬虫之Appium 的使用
搭建appium环境,appium基本使用,API操作等等
46 0
|
4月前
|
数据采集 Web App开发 存储
Selenium库编写爬虫详细案例
Selenium库编写爬虫详细案例
|
2月前
|
数据采集 Web App开发 前端开发
Python爬虫之自动化测试Selenium#7
Selenium基本使用、查找结点、节点交互、动作链、获取节点信息、延时等待、前进后退、Cookies、选项卡管理、异常处理【2月更文挑战第26天】
46 1
Python爬虫之自动化测试Selenium#7
|
2月前
|
Web App开发 数据采集 前端开发
Python Selenium 爬虫淘宝案例
本文基于Selenium + MongoDB + ChromeDriver + Pyquery实现爬虫淘宝案例。【2月更文挑战第11天】
100 1
Python Selenium 爬虫淘宝案例
|
8月前
|
Web App开发 数据采集 JavaScript
|
8月前
|
XML 前端开发 机器人
Selenium工具学习
Selenium工具学习
63 1
|
11月前
|
数据采集 Python
Python爬虫-selenium
Python爬虫-selenium
|
JavaScript
web自动化之selenium的特殊用法(一)
文章目录 web自动化之selenium的特殊用法(一) 1、get_attribute() 2、js滚动页面 3、Tab键点击页面未展示元素 4、通过空格键执行页面滚动操作 1.摁空格键 2.报错:TypeError: list indices must be integers or slices, not WebElement ———————————————— 版权声明:本文为CSDN博主「梦无矶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_46158060/article/details/
311 0
|
数据采集 Web App开发 JavaScript
|
Web App开发 数据采集 前端开发
「Python」爬虫-4.selenium的使用
> 本文主要讲解了selenium的基础使用,主要包括了窗口切换,以及遇到iframe和无头浏览器的设置。
294 0