Python爬虫:selenium模块基本使用
2023-09-14 09:07:13 时间
参考文档:
Selenium with Python英文文档
Selenium with Python中文翻译文档
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
# 声明浏览器对象
driver = webdriver.Chrome()
# 可用的浏览器对象:
# Firefox,Chrome,Ie,Edge,Opera,Safari,BlackBerry,PhantomJS,Android
# 访问页面
def foo1():
driver.get("http://www.baidu.com")
print(driver.page_source)
# 查找元素
try:
p=driver.find_element_by_class_name("p")
except NoSuchElementException as e:
print(e)
"""
# 单个元素
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_tag_name
find_element_by_link_text
find_element_by_class_name
find_element_by_css_selector
find_element_by_partial_link_text
# 多个元素
find_elements_by_id
find_elements_by_name
find_elements_by_xpath
find_elements_by_tag_name
find_elements_by_link_text
find_elements_by_class_name
find_elements_by_css_selector
find_elements_by_partial_link_text
"""
# 通用查找方式
from selenium.webdriver.common.by import By
driver.find_element(By.ID, "kw")
"""
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"
"""
# 元素交互
def foo2():
import time
driver.get("http://www.baidu.com")
input = driver.find_element_by_id("kw")
input.send_keys("淘宝")
time.sleep(2)
input.clear()
input.send_keys("京东")
button = driver.find_element_by_id("su")
time.sleep(2)
button.click()
# 交互动作
# 将动作附加到动作链中串行执行
def foo3():
driver.get("http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
# 切换到子frame
driver.switch_to.frame("iframeResult")
draggable = driver.find_element_by_id("draggable")
droppable = driver.find_element_by_id("droppable")
actions = webdriver.ActionChains(driver)
actions.drag_and_drop(draggable, droppable)
actions.perform()
# 切换回父frame
driver.switch_to.parent_frame()
# 执行javascript
def foo4():
driver.execute_script('alert("hello")')
# 获取文本和属性
def foo5():
driver.get("https://www.zhihu.com/topics")
# 隐式等待
driver.implicitly_wait(10)
# 显示等待
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
logo = wait.until(EC.presence_of_element_located((By.ID, "zh-top-link-logo")))
print(logo.text)
# 获取属性, id, 标签名, 位置, 大小
print(logo.get_attribute("class"))
print(logo.id)
print(logo.location)
print(logo.tag_name)
print(logo.size)
# 前进后退
def foo6():
import time
driver.get("http://www.taobao.com")
driver.get("http://www.jd.com")
driver.get("http://www.qq.com")
time.sleep(2)
driver.back()
time.sleep(2)
driver.forward()
time.sleep(3)
# cookie
def foo7():
driver.get("https://www.zhihu.com/explore")
print(driver.get_cookies())
driver.add_cookie({"name": "mouday", "value": "mouday.com"})
print(driver.get_cookies())
driver.delete_all_cookies()
print(driver.get_cookies())
# 选项卡管理
def foo8():
driver.get("https://www.qq.com/")
# 开启
driver.execute_script("window.open()")
print(driver.window_handles)
# 切换
driver.switch_to_window(driver.window_handles[1])
driver.get("https://www.taobao.com/")
time.sleep(2)
# 切换
driver.switch_to_window(driver.window_handles[0])
driver.get("https://www.jd.com/")
time.sleep(2)
# 异常处理
def foo9():
try:
driver.get("https://www.google.com")
except TimeoutException as e:
print(e.msg)
try:
p = driver.find_element_by_class_name("p")
except NoSuchElementException as e:
print(e)
# 关闭退出浏览器
driver.close()
driver.quit()
# 如果不退出浏览器,会报错:
# ImportError: sys.meta_path is None, Python is likely shutting down
相关文章
- [Python 爬虫]煎蛋网 OOXX 妹子图爬虫(2)——多线程+多进程下载图片
- Python获取时间戳_python爬虫时间戳
- Python爬虫之requests
- python爬虫的4个实例
- 送你情人节(劫)专属Python全套代码
- python字符串转化列表_Python列表到字符串的转换[通俗易懂]
- python问题 Traceback (most recent call last)
- java和python哪个值得学-学java好还是Python好?
- Python处理CSV文件(一)
- python语言一般用于什么_PYthon
- 【说站】如何用python画月饼
- 【说站】python ChainMap如何管理映射列表
- 【说站】python如何创建数值列表
- mac系统pycharm配置python环境_mac python开发环境
- python deepcopy函数_Python deepcopy
- 盘点一个Python网络爬虫实战问题
- Python爬虫之selenium学习
- Python爬虫学习:Cookie 和 Session 的区别是什么?
- 【2022新书】数据可视化与Python和JavaScript
- python-Python与SQLite数据库-SQLite数据库的基本知识(一)
- 一组python用于验证数据的函数详解编程语言
- Python操作Execl 实现自动化填表详解编程语言
- Linux系统快速运行Python脚本(linux运行python脚本)
- Python结合MySQL实现信息交互(python与mysql交互)
- Python轻松连接SQL Server数据库(python连接sqlserver)
- Python如何使用MySQL构建立连接(python怎么连接mysql)
- Python操作MySQL数据库的必备模块mysqlpython(mysql_python)