当前位置:网站首页>22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
2022-07-03 18:35:00 【Daily leisurely King】
Try B Station login , The problems encountered are roughly as follows .
I learned from Cui Qingcai 《python3 Web crawler development practice 》
1.EC Brackets are required in the display waiting module
2.for List form of loop
3. Report errors :'click_image' object has no attribute 'wait'
4. Report errors :chromedriver' executable needs to be in PATH
5. Report errors :presence_of_element_located() takes 1 positional argument but 2 were given
6. Report errors :int() argument must be a string, a bytes-like object or a number, not 'list'
7.selenium The screenshot deviates
8.crop() Function clipping error
9.BytesIO() Usage of
10.file.getvalue() Usage of
( One )1.EC Brackets are required in the display waiting module
EC Display module , Look at the code
def login_in(self):
self.browser.get(self.url)
self.browser.maximize_window()
username = self.wait.until(EC.presence_of_element_located((By.ID, 'login-username' )))
password = self.wait.until(EC.presence_of_element_located((By.ID, 'login-passwd' )))
button = self.wait.until(EC.element_to_be_clickable((By.LINK_TEXT,' Sign in ')))
username.send_keys(bilibili_username)
password.send_keys(bilibili_password)
button.click()
time.sleep(2)Be careful EC.presence_of_element_located() In this function, you need to write
EC.presence_of_element_located((By.ID, 'login-username')) There needs to be a bracket inside
( Two )2.for List form of loop
for There are two ways of cycling , The writing method encountered this time is like this
def get_points(self,captch_result):
'''
:param captch_result: Super Eagle output
:return:
'''
groups = captch_result.get('pic_str').split('|')
locations = [[int(number) for number in group.split(',')] for group in groups]
return locationsamong int(number) It's the output ,
for number in group.split(',') It's an inner loop for group in groups It's the outer cycle
However, I'm not very clear about the output content , To be continued .
( 3、 ... and ) Report errors :'click_image' object has no attribute 'wait'
EC According to wait
class click_image():
def first_login(self):
self.url = 'https://passport.bilibili.com/login'
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser,20)
self.chaojiying = Chaojiying(chaojiying_username,chaojiying_password,chaojiying_soft_id)It started like this , Later, he kept telling me mistakes , I changed the code to
class click_image():
def __init__(self):
self.url = 'https://passport.bilibili.com/login'
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser,20)
self.chaojiying = Chaojiying(chaojiying_username,chaojiying_password,chaojiying_soft_id)Change the function name to __init__(self): That's all right.
( Four )chromedriver' executable needs to be in PATH
This just needs to be chromedriver.exe Put in separately contain chrome.exe and python37 In the folder of , then chromedriver.exe Put the system into the environment variable PATH Then you can , as follows


Then right click my computer - attribute - Advanced system setup - environment variable - Double click... In the system variable path- newly build , take
The address in the above figure :C:\Users\15466\AppData\Local\Google\Chrome\Application
After putting it in , Just make sure all the way .
( 5、 ... and )presence_of_element_located() takes 1 positional argument but 2 were given
The solution is the first one mentioned
( 6、 ... and )int() argument must be a string, a bytes-like object or a number, not 'list'
def touch_click(self,locations):
for location in locations:
print(location)
ActionChains(self.browser).move_to_element_with_offset(self.get_image(),location[0],
location[1]).click().perform()
time.sleep(1)↑↑↑ This is right .
The reason why I made a mistake at that time was :
ActionChains(self.browser).move_to_element_with_offset(self.get_image(),locations[0],
locations[1]).click().perform()Yes , Look carefully and you'll find that I'm location A is added after s
( 7、 ... and )selenium The screenshot deviates
1. Zoom ratio of computer system

I am here 125% , So just change it to 100%, Then restart the computer
2. Multiply the following coordinates by 1.25( as follows )
def crop_screenshot(self,name='captch.png'):
top,bottom,left,right= self.get_image_location()
screenshot = self.get_screenshot()
captch = screenshot.crop((left*1.25,top*1.25,right*1.25,bottom*1.25))
captch.save(name)
return captch3. One more , Anyway, I'm too lazy to watch
( 8、 ... and )crop() Function clipping error
def crop_screenshot(self,name='captch.png'):
top,bottom,left,right= self.get_image_location()
screenshot = self.get_screenshot()
captch = screenshot.crop((left,top,right,bottom))
captch.save(name)
return captchcrop(x,y,x+width,y+height)
But at the beginning, what the brain said was
captch = screenshot.crop((top,bottom,left,right))
But it makes the child sad .
( Nine )BytesIO() Usage of
BytesIO It can read and write in memory bytes
Sure
file = BytesIO()
To create a folder
( Ten )file.getvalue() Usage of
Can anyone explain , I don't understand ...
Attach code :
from PIL import Image
from pach.chaojiying_Python.chaojiying import Chaojiying
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from io import BytesIO
''' preparation '''
chaojiying_username = ' Super Eagle username '
chaojiying_password = ' Super Eagle code '
chaojiying_soft_id = ' Software ID'
chaojiying_kind = ' Verification code type '
bilibili_username = 'B Station account '
bilibili_password = 'B Station password '
class click_image():
def __init__(self):
self.url = 'https://passport.bilibili.com/login'
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser,20)
self.chaojiying = Chaojiying(chaojiying_username,chaojiying_password,chaojiying_soft_id)
def login_in(self):
self.browser.get(self.url)
self.browser.maximize_window()
username = self.wait.until(EC.presence_of_element_located((By.ID, 'login-username' )))
password = self.wait.until(EC.presence_of_element_located((By.ID, 'login-passwd' )))
button = self.wait.until(EC.element_to_be_clickable((By.LINK_TEXT,' Sign in ')))
username.send_keys(bilibili_username)
password.send_keys(bilibili_password)
button.click()
time.sleep(2)
def close(self):
self.browser.close()
def get_image(self):
images = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_item_img')))
return images
def get_image_location(self):
images = self.get_image()
location = images.location
size = images.size
top,bottomm,left,right = location['y'],location['y']+size['height'],location['x'],location['x']+size['width']
return (top,bottomm,left,right)
def get_screenshot(self):
screenshot = self.browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot
def crop_screenshot(self,name='captch.png'):
top,bottom,left,right= self.get_image_location()
screenshot = self.get_screenshot()
captch = screenshot.crop((left,top,right,bottom))
captch.save(name)
return captch
def get_points(self,captch_result):
'''
:param captch_result: Super Eagle output
:return:
'''
groups = captch_result.get('pic_str').split('|')
locations = [[int(number) for number in group.split(',')] for group in groups]
return locations
def touch_click(self,locations):
for location in locations:
print(location)
ActionChains(self.browser).move_to_element_with_offset(self.get_image(),location[0],
location[1]).click().perform()
time.sleep(1)
def login_click(self):
button = self.wait.until(EC.element_to_be_clickable((By.LINK_TEXT,' confirm ')))
button.click()
def bilibili_login(self):
self.login_in()
captch = self.crop_screenshot()
file = BytesIO()
captch.save(file , format='PNG' )
result = self.chaojiying.post_pic(file.getvalue(),chaojiying_kind)
print(result)
locations = self.get_points(result)
self.touch_click(locations)
self.login_click()
if __name__ == '__main__':
login = click_image()
login.bilibili_login()边栏推荐
- Grammaire anglaise Nom - Classification
- Sensor 调试流程
- Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- [combinatorics] generating function (commutative property | derivative property | integral property)
- Enterprise custom form engine solution (12) -- form rule engine 2
- Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
- Theoretical description of linear equations and summary of methods for solving linear equations by eigen
- 204. Count prime
- How to analyze the rising and falling rules of London gold trend chart
- [tutorial] build your first application on coreos
猜你喜欢

Computer graduation design PHP sports goods online sales system website

Redis cache avalanche, penetration, breakdown
![网格图中递增路径的数目[dfs逆向路径+记忆dfs]](/img/57/ff494db248171253996dd6c9110715.png)
网格图中递增路径的数目[dfs逆向路径+记忆dfs]

12、 Service management

Redis core technology and practice - learning notes (IX): slicing cluster

Raft 日志复制

How to analyze the rising and falling rules of London gold trend chart

Module 9 operation

Ping problem between virtual machine and development board

Summary and Reflection on the third week of winter vacation
随机推荐
SQL injection -day16
G1 garbage collector of garbage collector
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Win32: analyse du fichier dump pour la défaillance du tas
Sensor 调试流程
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
How to analyze the rising and falling rules of London gold trend chart
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
2022-2028 global lithium battery copper foil industry research and trend analysis report
[linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
2022-2028 global petroleum pipe joint industry research and trend analysis report
Torch learning notes (5) -- autograd
Software development freelancer's Road
Niuke monthly race 31 minus integer
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
Valentine's day, send you a little red flower~
041. (2.10) talk about manpower outsourcing
12、 Service management
SDNUOJ1015