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()

相关文章
|
5天前
|
数据采集 XML 数据处理
使用Python实现简单的Web爬虫
本文将介绍如何使用Python编写一个简单的Web爬虫,用于抓取网页内容并进行简单的数据处理。通过学习本文,读者将了解Web爬虫的基本原理和Python爬虫库的使用方法。
|
2天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
9 1
|
3天前
|
数据采集 Web App开发 数据可视化
Python爬虫技术与数据可视化:Numpy、pandas、Matplotlib的黄金组合
Python爬虫技术与数据可视化:Numpy、pandas、Matplotlib的黄金组合
|
4天前
|
Python 容器
python内置函数、数学模块、随机模块(二)
python内置函数、数学模块、随机模块(二)
|
4天前
|
索引 Python
python内置函数、数学模块、随机模块(一)
python内置函数、数学模块、随机模块(一)
|
4天前
|
数据采集 存储 大数据
Python爬虫:数据获取与解析的艺术
本文介绍了Python爬虫在大数据时代的作用,重点讲解了Python爬虫基础、常用库及实战案例。Python因其简洁语法和丰富库支持成为爬虫开发的优选语言。文中提到了requests(发送HTTP请求)、BeautifulSoup(解析HTML)、Scrapy(爬虫框架)、Selenium(处理动态网页)和pandas(数据处理分析)等关键库。实战案例展示了如何爬取电商网站的商品信息,包括确定目标、发送请求、解析内容、存储数据、遍历多页及数据处理。最后,文章强调了遵守网站规则和尊重隐私的重要性。
15 2
|
6天前
|
人工智能 安全 Java
Python 多线程编程实战:threading 模块的最佳实践
Python 多线程编程实战:threading 模块的最佳实践
123 5
|
6天前
|
人工智能 数据库 开发者
Python中的atexit模块:优雅地处理程序退出
Python中的atexit模块:优雅地处理程序退出
8 3
|
8天前
|
数据采集 定位技术 Python
Python爬虫IP代理技巧,让你不再为IP封禁烦恼了! 
本文介绍了Python爬虫应对IP封禁的策略,包括使用代理IP隐藏真实IP、选择稳定且数量充足的代理IP服务商、建立代理IP池增加爬虫效率、设置合理抓取频率以及运用验证码识别技术。这些方法能提升爬虫的稳定性和效率,降低被封禁风险。
|
9天前
|
存储 开发者 Python
Python中的argparse模块:命令行参数解析的利器
Python中的argparse模块:命令行参数解析的利器
16 2