Selenium和requests的简单结合实现
Project description
Husky Spider utils
介绍
本库简单实现了 Selenium 和 requests 的结合,并封装了少部分常用 Selenium 功能。使用 SeleniumSession 相关方法会自动更新 cookies(session 和 selenium 互通)。
使用
from husky_spider_utils import SeleniumSession
session = SeleniumSession(selenium_init_url="https://cn.bing.com")
session.selenium_get("https://cn.bing.com")
方法说明
__init__ 方法
- 功能:初始化
SeleniumSession对象。 - 参数:
selenium_init_url:Selenium 初始化时访问的 URL,默认为"https://cn.bing.com"。driver_type:使用的浏览器类型,可选值为"firefox"、"chrome"、"edge",默认为"firefox"。headers:自定义请求头,若不传则使用默认请求头。
init_headers 方法
- 功能:初始化请求头。
- 参数:
headers:自定义请求头,若不传则使用默认请求头。
init_driver 方法
- 功能:初始化 WebDriver。
- 参数:
driver_type:使用的浏览器类型,可选值为"firefox"、"chrome"、"edge",默认为"firefox"。
get 方法
- 功能:使用 requests 发起 GET 请求。
- 参数:
url:请求的 URL。**kwargs:其他参数,如params、timeout等。
- 返回值:请求的响应对象。
post 方法
- 功能:使用 requests 发起 POST 请求。
- 参数:
url:请求的 URL。data:请求体中的数据。json:请求体中的 JSON 数据。**kwargs:其他参数,如timeout等。
- 返回值:请求的响应对象。
request 方法
- 功能:使用 requests 发起请求。
- 参数:
url:请求的 URL。method:请求方法,如"GET"、"POST"等。**kwargs:其他参数,如data、json、timeout等。
- 返回值:请求的响应对象。
get_session_cookies_to_dict 方法
- 功能:将 session 的 cookies 转换为字典格式。
- 返回值:cookies 字典。
cookies_to_driver 方法
- 功能:将 session 的 cookies 添加到 WebDriver 中,并刷新页面。
selenium_get 方法
- 功能:使用 WebDriver 访问指定的 URL。
- 参数:
url:访问的 URL。
selenium_cookies_to_session 方法
- 功能:将 WebDriver 的 cookies 添加到 session 中。
send_key 方法
- 功能:在指定的元素中输入内容。
- 参数:
value:元素的定位值。send_value:输入的内容。by:元素的定位方式,默认为By.CSS_SELECTOR。timeout:超时时间,默认为 60 秒。
click 方法
- 功能:点击指定的元素。
- 参数:
value:元素的定位值。by:元素的定位方式,默认为By.CSS_SELECTOR。timeout:超时时间,默认为 60 秒。
hover 方法
- 功能:鼠标悬停在指定的元素上。
- 参数:
value:元素的定位值。by:元素的定位方式,默认为By.CSS_SELECTOR。timeout:超时时间,默认为 60 秒。
on_input 方法
- 功能:静态方法,用于获取用户输入。
- 参数:
des:输入提示信息。
scroll 方法
- 功能:滚动页面到指定的高度。
- 参数:
height:滚动的高度,默认为 200。
scroll_to_el 方法
- 功能:滚动页面到指定的元素。
- 参数:
element:指定的元素。
scroll_to_el_by_value 方法
- 功能:滚动页面到指定的元素。
- 参数:
value:元素的定位值。by:元素的定位方式,默认为By.CSS_SELECTOR。
scroll_to_top 方法
- 功能:滚动页面到顶部。
scroll_to_bottom_fade 方法
- 功能:平滑滚动页面到底部。
- 参数:
step:每次滚动的距离(像素),默认为 100。max_height:最大滚动高度(像素),默认为页面底部。
scroll_to_bottom 方法
- 功能:将页面滚动到最底部。
save_cookies 方法
- 功能:保存 WebDriver 的 cookies 到指定的文件。
- 参数:
save_path:保存文件的路径。
load_cookies 方法
- 功能:加载指定文件中的 cookies 到 WebDriver 和 session 中。
- 参数:
load_path:加载文件的路径。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file husky_spider_utils-0.1.4.tar.gz.
File metadata
- Download URL: husky_spider_utils-0.1.4.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d16b25dc5389ceb41c2cbed184fd6ba98744f1780240002b3e3ccdb18cd6dddf
|
|
| MD5 |
d06485171f4aed7f7747b1db882294c4
|
|
| BLAKE2b-256 |
cd4143b6cd07f677e496259e15f508ea7877d085b5e086e34ea48b230efea7fc
|
File details
Details for the file husky_spider_utils-0.1.4-py3-none-any.whl.
File metadata
- Download URL: husky_spider_utils-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c33513e8ce5b3128266efc65b3a0929578bfaebabeff77ecfbb72ae1ee911329
|
|
| MD5 |
bf6a92b3fe48f6ed3994185ed5d6d6e6
|
|
| BLAKE2b-256 |
ed175b3116e645e7f46f1591f644f25e23f1f0cbbffed9244ac8ccafef77efd2
|