- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python爬虫基础之selenium库的用法总结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
官网 。
总的来说: selenium库主要用来做浏览器的自动化脚本库.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from
selenium
import
webdriver
url
=
'http://www.baidu.com'
# 将webdriver实例化
path
=
'C:\Program Files (x86)\Python38-32\chromedriver.exe'
browser
=
webdriver.Chrome(executable_path
=
path)
# 用谷歌浏览器访问百度
r
=
browser.get(url)
with
open
(
'test.txt'
,
'wb+'
) as f:
f.write(r.content)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
'''
代码功能:selenium是的常用用法
时间:@Date: 2021-05-22 21:37:05
'''
from
selenium
import
webdriver
# 导入Options类
from
selenium.webdriver.chrome.options
import
Options
url
=
"https://movie.douban.com/"
# Options的实例化
chrome_options
=
Options()
# 设置浏览器参数
# --headless 是不显示浏览器启动以及执行过程
chrome_options.add_argument(
'--headless'
)
# 设置lang和User-Agent信息,防止反爬检测
chrome_options.add_argument(
'lang=zh_CN.utf-8'
)
UserAgent
=
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
chrome_options.add_argument(
'User-Agent='
+
UserAgent)
# 启动浏览器并设置chrome_options参数
driver
=
webdriver.Chrome(chrome_options
=
chrome_options)
# 设置浏览器窗口最大化
# driver.maximize_window()
# # 设置浏览器窗口最小化
# driver.minimize_window()
driver.get(url)
# 获取网页的标题
print
(driver.title)
# page_source是获取网页的HTML代码
print
(driver.page_source)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from
selenium
import
webdriver
import
time
# 启动浏览器
driver
=
webdriver.Chrome()
driver.get(
'https://www.youdao.com'
)
time.sleep(
5
)
# 添加cookie
driver.add_cookie({
'name'
:
'login'
,
'value'
:
'登录'
})
# 获取全部cookie
allCookies
=
driver.get_cookies()
print
(
'全部cookies'
,allCookies)
# 获取name为login的cookie
cookie
=
driver.get_cookie(
'login'
)
print
(
'name为login的cookie'
,cookie)
# 删除单个cookie
driver.delete_cookie(
'login'
)
print
(
"\n--------------剩余的cookies\n"
,driver.get_cookies())
# 删除全部cookies
driver.delete_all_cookies()
print
(
"-------------剩余的cookies------------\n"
,driver.get_cookies())
time.sleep(
60
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html>
<html lang
=
"en"
>
<head>
<meta charset
=
"UTF-8"
>
<meta http
-
equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
<meta name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<title>Document<
/
title>
<
/
head>
<body>
<h1>配合selenium模拟上传文件<
/
h1>
<
input
type
=
'file'
name
=
'file'
/
>
<
/
body>
<
/
html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
'''
代码功能:selenium上传文件 --配合upload.html使用
时间:@Date: 2021-05-23 09:56:53
'''
from
selenium
import
webdriver
import
time
driver
=
webdriver.Chrome()
url
=
'http://localhost:52330/selenium/upload.html'
driver.get(url)
ele
=
driver.find_element_by_name(
'file'
)
print
(
"获取到的元素"
,ele)
# 注意路径不能有中文字符
ele.send_keys(
'D:\dcsdk_eventv3.db'
)
time.sleep(
10
)
|
下载文件 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
'''
代码功能:模拟文件下载
时间:@Date: 2021-05-23 10:21:28
'''
from
selenium
import
webdriver
import
time
# 设置文件保存路径,如果不设置,会默认保存到Downloads文件夹
options
=
webdriver.ChromeOptions()
prefs
=
{
'download.default_directory'
:
'D:\\'
}
options.add_experimental_option(
'prefs'
,prefs)
# 启动浏览器
driver
=
webdriver.Chrome()
# 下载PC版微信
driver.get(
'https://pc.weixin.qq.com'
)
# 浏览器窗口最大化
driver.maximize_window()
time.sleep(
5
)
# 点击下载按钮
driver.find_element_by_class_name(
'download-button'
).click()
time.sleep(
30
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
from
selenium
import
webdriver
import
time
url
=
'https://www.baidu.com/'
driver
=
webdriver.Chrome()
# 隐式等待,一次设置对整个driver的周期都起作用
driver.implicitly_wait(
30
)
driver.get(url)
# 使用js开启新的窗口
js
=
'window.open("https://www.sogou.com/")'
driver.execute_script(js)
# 获取当前显示的窗口信息
current_window
=
driver.current_window_handle
print
(driver)
# 获取浏览器的全部窗口信息
handles
=
driver.window_handles
print
(
'获取到的窗口全部信息\n------------------\n'
,handles)
'''
获取到的窗口全部信息
------------------
['CDwindow-7FB808B4F24EF5385A9AFBDC21FA13B9', 'CDwindow-E879C0A64E734C3F88468A4388F48E3B']
'''
# 设置延时看切换的效果
time.sleep(
3
)
# 根据窗口信息进行窗口切换
# 切换到百度搜索的窗口
driver.switch_to_window(handles[
0
])
time.sleep(
3
)
# 切换到搜狗窗口
driver.switch_to_window(handles[
1
])
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
'''
代码功能:熟悉selenium的自动化操作
时间:2020/5/22
'''
from
selenium
import
webdriver
from
selenium.webdriver.common.keys
import
Keys
import
time
# Keys 类中定义了很多快捷键
url
=
'https://wwww.baidu.com'
path
=
'C:\Program Files (x86)\Python38-32\chromedriver.exe'
driver
=
webdriver.Chrome(executable_path
=
path)
driver.get(url)
# 获取输入框标签对象
element
=
driver.find_element_by_id(
'kw'
)
# 输入框输入内容
element.send_keys(
'python你'
)
time.sleep(
2
)
# 删除最后一个文字
element.send_keys(Keys.BACK_SPACE)
time.sleep(
2
)
# 添加空格加教程
element.send_keys(Keys.SPACE)
element.send_keys(
"教程"
)
time.sleep(
2
)
# ctrl+a 全选输入框内容
element.send_keys(Keys.CONTROL,
'a'
)
time.sleep(
2
)
# ctrl+x 剪切输入框内容
element.send_keys(Keys.CONTROL,
'x'
)
time.sleep(
2
)
# ctrl+v 复制
element.send_keys(Keys.CONTROL,
'v'
)
time.sleep(
2
)
# 回车键
driver.find_element_by_id(
'su'
).send_keys(Keys.ENTER)
time.sleep(
10
)
|
到此这篇关于Python爬虫基础之selenium库的用法总结的文章就介绍到这了,更多相关Python selenium库内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/qq_45704048/article/details/117202721 。
最后此篇关于Python爬虫基础之selenium库的用法总结的文章就讲到这里了,如果你想了解更多关于Python爬虫基础之selenium库的用法总结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Java 库和 android 库有什么区别,各自有什么优点/缺点? 最佳答案 您可以在 Android 应用程序中包含标准 Java .jar 文件库。它们在 .apk 构建时被翻译成 Dalvik
所以,我现在的代码就像从 Java 层加载库(比如 liba.so),并在内部 liba.so 加载 libb.so。因此,如果我必须将所有库打包到 APK 中并将其安装在没有 root 访问权限的设
我想在我的系统中设置 LEDA 库。 我已经从以下链接下载了 LEDA 库 http://www.algorithmic-solutions.info/free/d5.php Instruct
我想用 autoconf 创建一个共享库。但是,我希望共享库具有“.so”扩展名,而不是以“lib”开头。基本上,我想制作一个加载 dlopen 的插件。 .是否有捷径可寻? 当我尝试使用 autoc
我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB 我的代码: eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib
我正在构建一个使用以下 Boost header 的程序(我使用的是 Microsoft Visual C++ 10), #include #include #include #include
当我通过 cygwin 在 hadoop 上运行此命令时: $bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 我
我已经通过 vcpgk 成功安装了一个 C++ 库,名为:lmdb:x64-windows 我还安装了lmdb通过 Cabal 安装的 Haskell 绑定(bind)包 在尝试测试 lmdb 包时:
我该如何解决这个问题? 我刚刚将 javacv jar 文件复制到我的项目 Lib 文件夹下,但出现了这个错误! 我可以找到这个thread来自谷歌,但不幸的是,由于我国的谷歌限制政策,该页面无法打开
我有一个 Android 库项目 FooLib。 FooLib 引用 Android Context 之类的东西,但不需要任何资源文件(res/ 中的东西)所以我目前将其打包为供我的应用使用的 JAR
我正在开发一个 Android 应用程序(使用 Android Studio),它能够通过手势识别算法了解您正在进行的 Activity 。对于我使用 nickgillian ithub 帐户上可用的
关于从 .NET Framework 项目中引用 .NET Standard 类库的问题有很多类似的问题,其中 netstandard 库中的 NuGet 包依赖项不会流向 netframework
我已经从互联网上下载了 jna-4.2.2.jar,现在想将这个 jar 导入到我的项目中。但是当我试图将这个 jar 导入我的项目时,出现以下错误。 [2016-06-20 09:35:01 - F
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
进入 Web 开发有点困难。过去 5 年我一直致力于 winforms 工作。所以我正在努力从一种切换到另一种。前段时间,我使用过 JavaScript,但现在还没有大量的 JavaScript 库
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在寻找一个用Python编写的与logstash(ruby + java)类似的工具/库。 我的目标是: 从 syslog 中解析所有系统日志 解析应用程序特定日志(apache、django、m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我花了几天时间试图寻找用于 JavaPOS 实现的 .jar 库,但我找不到任何可以工作的东西。我找到了很多像这样的文档:http://jpos.1045706.n5.nabble.com/file/
这个问题在这里已经有了答案: Merge multiple .so shared libraries (2 个答案) 关闭 9 年前。 我有我在代码中使用的第三方库的源代码和对象。该库附带有关如何使
我是一名优秀的程序员,十分优秀!