Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
京
京东搜索采集自动验证
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄梓誉
京东搜索采集自动验证
Commits
4060af6c
Commit
4060af6c
authored
Feb 22, 2024
by
huangziyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
446cd288
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
56 deletions
+66
-56
京东搜索_自动版.py
京东搜索_自动版.py
+66
-56
No files found.
京东搜索_自动版.py
View file @
4060af6c
...
...
@@ -31,6 +31,7 @@ mysql_zdzs_proxy_user = 'zgcindex'
mysql_zdzs_proxy_password
=
'zgcprice2019'
mysql_zdzs_proxy_database
=
'zdzs_proxy'
import
config
username
=
config
.
username
# 登陆密码
password
=
config
.
password
...
...
@@ -188,14 +189,19 @@ def detect_circular_distortion(image_path, circle_radius=65):
return
average_distance
def
any_of_elements_present
(
*
locators
):
"""检查给定的任意一个元素是否出现在DOM中"""
def
_predicate
(
driver
):
for
locator
in
locators
:
if
EC
.
presence_of_element_located
(
locator
)(
driver
):
return
True
return
False
return
_predicate
def
find_best_match_angle
(
background_img_path
,
rotating_img_path
):
# 加载背景图片和验证码图片
background
=
Image
.
open
(
background_img_path
)
...
...
@@ -340,8 +346,8 @@ def get_cookie():
element
=
driver
.
find_element_by_class_name
(
"JDJRV-slide-btn"
)
# 获取元素的X和Y位置
x_position
=
element
.
location
[
'x'
]
+
50
y_position
=
element
.
location
[
'y'
]
+
100
x_position
=
element
.
location
[
'x'
]
+
50
y_position
=
element
.
location
[
'y'
]
+
100
print
(
"元素的X位置:"
,
x_position
)
print
(
"元素的Y位置:"
,
y_position
)
...
...
@@ -393,7 +399,8 @@ def get_cookie():
print
(
"网页加载完成"
)
text
=
driver
.
page_source
while_count
=
0
while
(
'快速验证'
in
text
and
'验证一下,购物无忧'
in
text
)
or
'前方拥挤,请刷新重试'
in
text
or
'class="title">加载中...</span></div></div><!----></div>'
in
text
:
while
(
'快速验证'
in
text
and
'验证一下,购物无忧'
in
text
)
or
'前方拥挤,请刷新重试'
in
text
or
'class="title">加载中...</span></div></div><!----></div>'
in
text
:
while_count
+=
1
if
while_count
>
1
:
driver
.
get
(
url
)
...
...
@@ -439,8 +446,8 @@ def get_cookie():
element
=
driver
.
find_element_by_class_name
(
"bg-blue"
)
# 获取元素的X和Y位置
x_position
=
element
.
location
[
'x'
]
+
50
y_position
=
element
.
location
[
'y'
]
+
100
x_position
=
element
.
location
[
'x'
]
+
50
y_position
=
element
.
location
[
'y'
]
+
100
print
(
"元素的X位置:"
,
x_position
)
print
(
"元素的Y位置:"
,
y_position
)
...
...
@@ -473,62 +480,65 @@ def get_cookie():
except
NoSuchElementException
:
# print("未查找到验证图片背景图,可能是旋转图片,换一种方式获取div")
try
:
background
=
driver
.
find_element
(
by
=
By
.
XPATH
,
value
=
'//*[@id="img-back-div"]'
)
background_url
=
background
.
get_attribute
(
"style"
)
.
replace
(
'"); height: 181px;'
,
""
)
.
replace
(
'background-image: url("'
,
""
)
# 用来找到登录图片的小滑块
slide
=
driver
.
find_element
(
by
=
By
.
XPATH
,
value
=
'//*[@id="img-rotate-div"]/img'
)
slide_url
=
slide
.
get_attribute
(
"src"
)
background_img
=
'background_img.png'
slide_img
=
'slide_img.png'
# 下载背景大图保存到本地
request
.
urlretrieve
(
background_url
,
background_img
)
# 下载滑块保存到本地
request
.
urlretrieve
(
slide_url
,
slide_img
)
best_angle
,
best_match_score
=
find_best_match_angle
(
background_img
,
slide_img
)
print
(
f
"最佳匹配角度为: {best_angle} 度"
)
# time.sleep(0.5)
# w1 = cv2.imread(background_img).shape[1]
# # 获取网页背景图宽度
# w2 = background.size['width']
x
=
228
/
360
*
best_angle
x
=
int
(
x
)
print
(
f
"移动: {x} "
)
# 找到要获取位置的元素,比如通过其ID
element
=
driver
.
find_element_by_id
(
"slider-div"
)
# 获取元素的X和Y位置
x_position
=
element
.
location
[
'x'
]
+
50
y_position
=
element
.
location
[
'y'
]
+
100
print
(
"元素的X位置:"
,
x_position
)
print
(
"元素的Y位置:"
,
y_position
)
# 其中x为屏幕左上角至滑块中心的横向像素值,y为屏幕左上角至滑块中心纵向像素值, 可根据自己屏幕配置
# time.sleep(0.5)
SlideUtils
.
slide_by_pyautogui2
(
x_position
,
y_position
,
x
+
2
,
offset_increase
)
try
:
WebDriverWait
(
driver
,
2
)
.
until
(
EC
.
presence_of_element_located
((
By
.
ID
,
"J_main"
))
)
text
=
driver
.
page_source
continue
except
:
while_count_2
=
0
while
while_count_2
<
5
:
while_count_2
+=
1
background
=
driver
.
find_element
(
by
=
By
.
XPATH
,
value
=
'//*[@id="img-back-div"]'
)
background_url
=
background
.
get_attribute
(
"style"
)
.
replace
(
'"); height: 181px;'
,
""
)
.
replace
(
'background-image: url("'
,
""
)
# 用来找到登录图片的小滑块
slide
=
driver
.
find_element
(
by
=
By
.
XPATH
,
value
=
'//*[@id="img-rotate-div"]/img'
)
slide_url
=
slide
.
get_attribute
(
"src"
)
background_img
=
'background_img.png'
slide_img
=
'slide_img.png'
# 下载背景大图保存到本地
request
.
urlretrieve
(
background_url
,
background_img
)
# 下载滑块保存到本地
request
.
urlretrieve
(
slide_url
,
slide_img
)
best_angle
,
best_match_score
=
find_best_match_angle
(
background_img
,
slide_img
)
print
(
f
"最佳匹配角度为: {best_angle} 度"
)
# time.sleep(0.5)
# w1 = cv2.imread(background_img).shape[1]
# # 获取网页背景图宽度
# w2 = background.size['width']
x
=
228
/
360
*
best_angle
x
=
int
(
x
)
print
(
f
"移动: {x} "
)
# 找到要获取位置的元素,比如通过其ID
element
=
driver
.
find_element_by_id
(
"slider-div"
)
# 获取元素的X和Y位置
x_position
=
element
.
location
[
'x'
]
+
50
y_position
=
element
.
location
[
'y'
]
+
100
print
(
"元素的X位置:"
,
x_position
)
print
(
"元素的Y位置:"
,
y_position
)
# 其中x为屏幕左上角至滑块中心的横向像素值,y为屏幕左上角至滑块中心纵向像素值, 可根据自己屏幕配置
# time.sleep(0.5)
SlideUtils
.
slide_by_pyautogui2
(
x_position
,
y_position
,
x
+
2
,
offset_increase
)
try
:
WebDriverWait
(
driver
,
5
)
.
until
(
EC
.
presence_of_element_located
((
By
.
CLASS_NAME
,
"verifyBt
n"
))
WebDriverWait
(
driver
,
2
)
.
until
(
EC
.
presence_of_element_located
((
By
.
ID
,
"J_mai
n"
))
)
except
NoSuchElementException
:
print
(
"网页加载异常"
)
text
=
driver
.
page_source
continue
break
except
:
try
:
WebDriverWait
(
driver
,
5
)
.
until
(
EC
.
presence_of_element_located
((
By
.
CLASS_NAME
,
"verifyBtn"
))
)
except
NoSuchElementException
:
print
(
"网页加载异常"
)
text
=
driver
.
page_source
break
finally
:
print
(
"网页加载完成"
)
finally
:
print
(
"网页加载完成"
)
finally
:
print
(
"网页加载完成"
)
text
=
driver
.
page_source
except
NoSuchElementException
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment