识别微博验证码 下载验证码无法打开的问题
来源:9-2 selenium模拟登录微博, 模拟鼠标下拉
慕丝7238044
2020-03-12
现在微博登录 需要验证在这里输入验证码 但是下载之后 验证码总是损坏 老师帮我看一下
headers = {
"HOST": "login.sina.com.cn",
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36"
}
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
browser = webdriver.Chrome('C:/Users/孙佩豪/AppData/Local/Google/Chrome/Application/chromedriver.exe',
chrome_options=chrome_options)
browser.get("https://www.weibo.com/")
time.sleep(10)
browser.find_element_by_xpath('//input[@id="loginname"]').send_keys("15292060685") # 输入密码
browser.find_element_by_xpath('//input[@type="password"]').send_keys("qq1362441") # 输入密码
try:
verifycode_image = browser.find_element_by_xpath('//img[@node-type="verifycode_image"]')
except:
verifycode_image = None
if verifycode_image:
image_url = verifycode_image.get_attribute("src")
image_content = requests.get(image_url).content
fh = open("./weibocode_en.jpeg", "wb") # 保存文件
fh.write(image_content)
Yundama = YDMHttp("", "", , "") # 实例化云打码
code = Yundama.decode("./weibocode_en.jpeg", 5000, 60) # 识别
while True: # 若识别失败 不停识别 直至成功
if code == "": #
code = Yundama.decode("yzm_en.jpeg", 5000, 60)
time.sleep(0.5)
else:
break
browser.find_element_by_xpath('//input[@node-type="verifycode"]').send_keys(code)
写回答
1回答
-
bobby
2020-03-13
import requests headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0" } pic = requests.get("https://login.sina.com.cn/cgi/pin.php?r=35959179&s=0&p=yf-73898da8e0f94a49ba31a58ca95428e24023", headers=headers) if pic.status_code == 200: file_path = "image.png" with open(file_path, 'wb') as fp: fp.write(pic.content) fp.close() print("下载完成")
这个代码我测试过可以下载图片啊
042021-04-14
相似问题