Swaiter
项目名字取自:Selenium+waiter
作为一个爬虫人,在使用selenium进行爬虫时,时长会遇到由于网络等原因导致无法及时获取一些元素,从而导致程序报错。此时聪明的小伙伴就会说了:加个time.sleep()不就好了。事实上这个方法确实可以解决绝大部分问题,但其也有一个比较尴尬的问题:到底要“睡”多久合适?长了耽误爬取效率,短了又怕没有作用,所以这个库就是为了解决上述问题而存在的。
安装
库的安装十分简单直接使用以下面的命令就可以安装成功了:
pip install Swaiter
使用
使用方法主要分为两种:直接使用基础组件进行开发、继承并添加自己的检索函数
使用基础组件
这里给一个简单的使用基础组件的例子:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from waiter import Waiter
if __name__ == '__main__':
# 获取selenium中驱动配置项(可省略)
driver_opt = Options()
url = "https://www.baidu.com/"
# 获取selenium中的driver对象
chrome_driver = Chrome(options=driver_opt)
chrome_driver.get(url=url)
# 利用获取到的driver对象创建Waiter对象
w = Waiter(chrome_driver)
# 传入xpath路径以及查询参数,检索input对象,并向其中填入查询参数
w.input_send_waiter(xpath="//input[@id='kw']", query="python")
# 利用xpath找到点击对象,模拟点击
w.click_waiter(xpath="//input[@id='su']")
# 在新的页面找到第一个链接,并获取其中的href属性值
result = w.elements_text_waiter(xpath="//h3/a[@tabindex=0]", attribute="href")
print(result)
上述例子中,使用了3个基础的搜索函数,向这样的搜索函数,本库一共提供了5个,应该可以满足简单的模拟需求,当然如果你有一些特别的搜索需求,也可以尝试使用第二种方式。
继承并开发自己的检索函数
from waiter import Waiter
from selenium.webdriver.common.by import By
class MyWaiter(Waiter):
def __init__(self, driver, interval, is_track, is_log):
super().__init__(driver=driver, interval=interval, is_track=is_track, is_log=is_log)
def _textarea_send(self, xpath, **kwargs):
self.driver.find_element(By.XPATH, xpath).send(kwargs)
def textarea_send(self, xpath, **kwargs):
self.wait(func="_textarea_send", xpath=xpath, interval=5, **kwargs)
环境
window10、window11
python>=3.8
selenium>=4.9.0
loguru>=0.7.0
版本
2023.5.22
Version-1.0.4
- 添加select组件的搜索器
- 添加获取组件所有选项
- 添加向select组件中输入值
2023.5.16
Version-1.0.3
完成基础功能
Metadata
Release files for Swaiter 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Swaiter-1.0.4.tar.gz | 5.2 kB | Without provenance | Details for Swaiter-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Swaiter-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for Swaiter-1.0.4-py3-none-any.whl |
Total release size: 10.7 kB
Release files / Swaiter-1.0.4.tar.gz
| Download URL | Swaiter-1.0.4.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1a2fcd757a8746b8b54e694086236c1e28435f5f1101b5f696879c03db56317
|
|
BLAKE2b-256 checksum How to use checksums |
1f5fe48b8b695b202c0b459e9785714e321e64fa5f1baf48b0ff7e67f7a8367c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.10
|
Release files / Swaiter-1.0.4-py3-none-any.whl
| Download URL | Swaiter-1.0.4-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
11f85c68c0edd5c53fb57c708a12926eb4fb128ea9c826887e0ae8831195fe9c
|
|
BLAKE2b-256 checksum How to use checksums |
45c7c828b3c8a87414a47139e8bcf60c1fa249024ccf0ad5508bb56ae57c5768
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.10
|
Security for Swaiter 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.