python爬虫(三)----Selenium

目录

[1. Selenium](#1. Selenium)

[1.1 Selenium是啥](#1.1 Selenium是啥)

[1.2 安装chrom Driver](#1.2 安装chrom Driver)

[1.3 selenium 使用](#1.3 selenium 使用)

[1.4 selenium元素定位](#1.4 selenium元素定位)

[1.5 访问元素信息](#1.5 访问元素信息)

[1.6 交互](#1.6 交互)

[2. Phantomjs、Chrom handless](#2. Phantomjs、Chrom handless)


1. Selenium

1.1 Selenium是啥

自动化Web浏览器操作

主要用于Web应用程序的测试

支持多操作系统、多浏览器(dirver)

支持无界面浏览器操作(自动交互)

1.2 安装chrome Driver

chrome deriver下载

需要注意 driver 与 chrom版本对应

1.3 selenium 使用

python 复制代码
# (1)导入selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# (2) 创建浏览器操作对象
path = 'chromedriver.exe'
# 创建 Service 对象
service = Service(executable_path=path)
# 创建 WebDriver 对象
browser = webdriver.Chrome(service=service)

# (3)访问网站
url = 'https://www.jd.com/'
browser.get(url)
# page_source获取网页源码
content = browser.page_source
print(content)

1.4 selenium元素定位

selenium3和selenium4的语法有一定不同

button = browser.find_element(By.ID, 'chat-submit-button')

button2 = browser.find_elements(By.ID, 'chat-submit-button')
ID = "id"

XPATH = "xpath"

LINK_TEXT = "link text"

PARTIAL_LINK_TEXT = "partial link text"

NAME = "name"

TAG_NAME = "tag name"

CLASS_NAME = "class name"

CSS_SELECTOR = "css selector"

python 复制代码
button = browser.find_element(By.ID, 'chat-submit-button')

button2 = browser.find_element(By.NAME, 'wd')

button3 = browser.find_elements(By.TAG_NAME, 'input')

button4 = driver.find_element(By.CLASS_NAME, "btn-primary")

button5 = browser.find_element(By.LINK_TEXT, '直播')

button6 = browser.find_element(By.PARTIAL_LINK_TEXT, '直')

button7 = browser.find_elements(By.XPATH, '//button[@id="chat-submit-button"]')

button8 = browser.find_element(By.CSS_SELECTOR, '#chat-submit-button')

print(button)

1.5 访问元素信息

获取元素属性

.get_attribute('class')

获取元素文本

.text

获取标签名

.tag_name

python 复制代码
input = browser.find_element(By.ID, 'chat-textarea')
# 获取标签的属性
print(input.get_attribute('class'))
# 获取标签的名字
print(input.tag_name)
# 获取元素文本
a = browser.find_element(By.LINK_TEXT,'新闻')
print(a.text)

1.6 交互

点击:click()

输入:send_keys()

后退操作:browser.back()

前进操作:browser.forword()

模拟JS滚动:

js='document.documentElement.scrollTop=100000'

browser.execute_script(js) 执行js代码

获取网页代码:page_source

退出:browser.quit()

2. Phantomjs、Chrome handless

无界面浏览器。不进行css渲染,运行效率高。

Phantomjs基本被淘汰,建议使用Chrom handless

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

def share_browser():
    # 替换为你的 chromedriver 路径
    path = 'chromedriver.exe'  # Windows 示例,如 chromedriver.exe
    service = Service(executable_path=path)
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')  # 无头模式
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    # 创建浏览器实例
    browser = webdriver.Chrome(service=service, options=options)
    return browser


browser = share_browser()

url = 'https://www.baidu.com'

browser.get(url)
browser.save_screenshot('baidu22.png')
相关推荐
动能小子ohhh20 小时前
django的URL路由配置常用方式
后端·python·django
AI 嗯啦21 小时前
Python 爬虫案例:爬取豆瓣电影 Top250 数据
开发语言·爬虫·python
云天徽上21 小时前
【数据可视化-104】安徽省2025年上半年GDP数据可视化分析:用Python和Pyecharts打造炫酷大屏
开发语言·python·信息可视化·数据分析·数据可视化
深瞳智检21 小时前
深度学习环境搭建运行(一) Ubuntu22.04 系统安装 CUDA11.8 和 CUDNN8.6.0 详细步骤(新手入门)
人工智能·python·深度学习·yolo·计算机视觉
大学生毕业题目21 小时前
毕业项目推荐:64-基于yolov8/yolov5/yolo11的蝴蝶种类检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·蝴蝶检测
m0_578267861 天前
从零开始的python学习(九)P134+P135+P136+P137+P138+P139+P140
开发语言·python·学习
@TsUnAmI~1 天前
基于Flask的企业级产品信息管理系统技术实现笔记
笔记·python·flask
程序员的世界你不懂1 天前
【Flask】测试平台开发,开发实现应用搜索和分页-第十篇
后端·python·flask
程序员的世界你不懂1 天前
【Flask】测试平台开发,实现全局邮件发送工具 第十二篇
网络·python·flask
软糖工程0011 天前
python中的分代垃圾回收机制的原理【python进阶二、2】
python·算法