Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
1_bit
S-AnalyseToolDemo
提交
85758e81
S
S-AnalyseToolDemo
项目概览
1_bit
/
S-AnalyseToolDemo
通知
256
Star
3
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
S-AnalyseToolDemo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
85758e81
编写于
9月 13, 2020
作者:
1_bit
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new file
上级
7bf2b9f5
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
84 addition
and
0 deletion
+84
-0
Browser.py
Browser.py
+84
-0
未找到文件。
Browser.py
0 → 100644
浏览文件 @
85758e81
from
selenium
import
webdriver
from
bs4
import
BeautifulSoup
from
SearchEngine
import
EngineConfManage
from
selenium.webdriver.support.wait
import
WebDriverWait
from
selenium.webdriver.support
import
expected_conditions
as
EC
from
selenium.webdriver.common.by
import
By
import
hashlib
import
time
import
xlwt
class
Browser
:
def
__init__
(
self
,
conf
):
self
.
browser
=
webdriver
.
Chrome
()
self
.
conf
=
conf
self
.
conf
[
'kw'
]
=
''
self
.
engine_conf
=
EngineConfManage
().
get_Engine_conf
(
conf
[
'engine'
]).
get_conf
()
#搜索内容设置
def
set_kw
(
self
,
kw
):
self
.
conf
[
'kw'
]
=
kw
#搜索内容写入到搜素引擎中
def
send_keyword
(
self
):
input
=
self
.
browser
.
find_element_by_id
(
self
.
engine_conf
[
'searchTextID'
])
input
.
send_keys
(
self
.
conf
[
'kw'
])
#搜索框点击
def
click_search_btn
(
self
):
search_btn
=
self
.
browser
.
find_element_by_id
(
self
.
engine_conf
[
'searchBtnID'
])
search_btn
.
click
()
#获取搜索结果与文本
def
get_search_res_url
(
self
):
res_link
=
{}
WebDriverWait
(
self
.
browser
,
timeout
=
30
,
poll_frequency
=
1
).
until
(
EC
.
presence_of_element_located
((
By
.
ID
,
"page"
)))
#内容通过 BeautifulSoup 解析
content
=
self
.
browser
.
page_source
soup
=
BeautifulSoup
(
content
,
"html.parser"
)
search_res_list
=
soup
.
select
(
'.'
+
self
.
engine_conf
[
'searchContentHref_class'
])
while
len
(
res_link
)
<
self
.
conf
[
'target_page'
]:
for
el
in
search_res_list
:
js
=
'window.open("'
+
el
.
a
[
'href'
]
+
'")'
self
.
browser
.
execute_script
(
js
)
handle_this
=
self
.
browser
.
current_window_handle
#获取当前句柄
handle_all
=
self
.
browser
.
window_handles
#获取所有句柄
handle_exchange
=
None
#要切换的句柄
for
handle
in
handle_all
:
#不匹配为新句柄
if
handle
!=
handle_this
:
#不等于当前句柄就交换
handle_exchange
=
handle
self
.
browser
.
switch_to
.
window
(
handle_exchange
)
#切换
real_url
=
self
.
browser
.
current_url
if
real_url
in
self
.
conf
[
'white_list'
]:
#白名单
continue
time
.
sleep
(
1
)
res_link
[
real_url
]
=
self
.
browser
.
page_source
#结果获取
self
.
browser
.
close
()
self
.
browser
.
switch_to
.
window
(
handle_this
)
content_md5
=
hashlib
.
md5
(
self
.
browser
.
page_source
.
encode
(
encoding
=
'UTF-8'
)).
hexdigest
()
#md5对比
self
.
click_next_page
(
content_md5
)
return
res_link
#下一页
def
click_next_page
(
self
,
md5
):
WebDriverWait
(
self
.
browser
,
timeout
=
30
,
poll_frequency
=
1
).
until
(
EC
.
presence_of_element_located
((
By
.
ID
,
"page"
)))
#百度搜索引擎翻页后下一页按钮 xpath 不一致 默认非第一页xpath
try
:
next_page_btn
=
self
.
browser
.
find_element_by_xpath
(
self
.
engine_conf
[
'nextPageBtnID_xpath_s'
])
except
:
next_page_btn
=
self
.
browser
.
find_element_by_xpath
(
self
.
engine_conf
[
'nextPageBtnID_xpath_f'
])
next_page_btn
.
click
()
#md5 进行 webpag text 对比,判断是否已翻页 (暂时使用,存在bug)
i
=
0
while
md5
==
hashlib
.
md5
(
self
.
browser
.
page_source
.
encode
(
encoding
=
'UTF-8'
)).
hexdigest
():
#md5 对比
time
.
sleep
(
0.3
)
#防止一些错误,暂时使用强制停止保持一些稳定
i
+=
1
if
i
>
100
:
return
False
return
True
class
BrowserManage
(
Browser
):
#打开目标搜索引擎进行搜索
def
search
(
self
):
self
.
browser
.
get
(
self
.
engine_conf
[
'website'
])
#打开搜索引擎站点
self
.
send_keyword
()
#输入搜索kw
self
.
click_search_btn
()
#点击搜索
return
self
.
get_search_res_url
()
#获取web页搜索数据
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录