3

我正在使用 python 3.7 和 requests-html 库。

我试图在会话中将获取请求发送到带有表单的站点。首先,我使用响应来获取 CAPTCHA 图像并下载它,然后在同一会话中发送一个 POST 请求,包括解码的 CAPTCHA 代码。发送获取请求并获取“ProcessKey”和验证码图像的第一部分效果很好。

出于某种原因,我发送 POST 请求的第二部分不断将我重定向到上一页并且它无法正常工作。

正如您在我的代码中看到的那样,我尝试将用户代理和请求标头更改为与我使用 chrome 开发面板获得的类似。

在我使它与 Selenium 库一起工作之前,但它不适合使用。

from requests_html import HTMLSession
import time


url = 'https://www.misim.gov.il/svinfonadlan2010/'
url2 = 'https://www.misim.gov.il/svinfonadlan2010/startpageNadlanNewDesign.aspx?ProcessKey='
url3 = 'https://www.misim.gov.il/svinfonadlan2010/InfoNadlanPerutWithMap.aspx?ProcessKey='

session = HTMLSession()
request = session.get(url)

process_key = request.url.split('ProcessKey=')[1]

# Get the captcha image code:
image_url = request.html.find('#ContentUsersPage_RadCaptcha1_CaptchaImageUP', first=True)
image_url = url + image_url.attrs['src']
image_file_name = process_key + '.png'
with open('captcha_temp_files/' + image_file_name, 'wb') as f:
    f.write(session.get(image_url).content)

print(request.url)
ans = input('Enter the captcha: ')

all_inputs = request.html.find('input')

data = {}
for i in all_inputs:
    if 'value' in i.attrs.keys():
        data[i.attrs['name']] = i.attrs['value']
    else:
        data[i.attrs['name']] = None

data["ctl00$ContentUsersPage$rbYeshuvOrGush"] = "rbMegush"
data['ctl00$ContentUsersPage$txtmegusha'] = 30010
data['ctl00$ContentUsersPage$txthelka'] = 129
data['ctl00$ContentUsersPage$txtadGush'] = 30010
data['ctl00$ContentUsersPage$txtadHelka'] = 129
data['ctl00$ContentUsersPage$DDLTypeNehes'] = 1
data['ctl00$ContentUsersPage$DDLMahutIska'] = 999
data['ctl00$ContentUsersPage$RadCaptcha1$CaptchaTextBox'] = ans


post_request_header = {
    "Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9,he-IL;q=0.8,he;q=0.7",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    #"Content-Length": "10663",
    "Content-Type": "application/x-www-form-urlencoded",
    "DNT": "1",
    "Host": "www.misim.gov.il",
    "Origin": "https://www.misim.gov.il",
    "Referer": url2 + process_key,
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36",
}


session.headers = post_request_header
request2 = session.post(url=url2 + process_key, data=data)
print(request2.url)
time.sleep(2)

request3 = session.get(url=url3 + process_key)
print(request3.url)

请帮助我了解这里出了什么问题,或者除了 Selenium 之外是否还有其他库可以做到这一点

先感谢您!

4

0 回答 0