import requests
from lxml import etree
import base64
from PIL import Image
session = requests.Session()
url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
page_text = session.get(url=url, headers=headers,).text
tree = etree.HTML(page_text)
img_src = 'https://so.gushiwen.org' + tree.xpath('//*[@id="imgCode"]/@src')[0]
res = requests.get(url=img_src).content
with open('./code.gif','wb') as f:
f.write(res)
Image.open('code.gif').save('code.png')
'''
通用文字識別
'''
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=L9BNPAgFmP7RRB4D0cFlhGUi&client_secret=eqE1iCj0kcMk53lpjMv3KbBoItX5MrkG'
response = requests.get(host).json()
access_token = response['access_token']
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
# 二進制方式打開圖片檔案
f = open('code.png', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
# access_token = host
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=https://bbs.csdn.net/topics/params, headers=headers).json()
code = response['words_result'][0]['words']
print(code)
Login_url = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx'
data = {
'__VIEWSTATE': 'Em4iCpnL3M3DrcgCu77COw33yUvSBkBcNV9Alv2+79sgFsGVhqyvPSI7LNRUv6yr3Zl1ZhvOHez60jaQmRF2sSBVKPNxXmrUeZgX1XGyQF1If1pVWgucP8Ivg+k=',
'__VIEWSTATEGENERATOR': 'C93BE1AE',
'from': 'http://so.gushiwen.org/user/collect.aspx',
'email': '[email protected]',
'pwd': 'zdy0519',
'code': code,
'denglu': '登錄'
}
page_text = session.post(url=Login_url, headers=headers, data=https://bbs.csdn.net/topics/data).text
with open('gushiwen.html','w',encoding='utf-8') as f:
f.write(page_text)
這是我模擬登陸一個古詩網的代碼,但是一直提示驗證碼錯誤,我看了看百度云識別出來的驗證碼沒錯啊,請問問題在哪兒
uj5u.com熱心網友回復:
headers里面應該需要添加手動驗證通過后生成的cookies資訊uj5u.com熱心網友回復:
我用session就是獲取了cookie啊 而且我也試了試在headers里面添加cookie 也是不行uj5u.com熱心網友回復:
解決了,圖片獲取那里應該用session,我沒用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/51304.html
