requests-html操作表单,Submit (POST) form with the python requests-html library fails

I'm using python 3.7 and the requests-html library.

I have tried to send a get request in a session to a site with a form. First I use the response to get the CAPTCHA image and download it, and than send a POST request in the same session including the decoded CAPTCHA code.

The first part of sending the get request and getting a "ProcessKey" and the CAPTCHA image works great.

For some reason the second part where I'm send the POST request keeps redirect me to the previous page and it's not working properly.

I tried to change the user agent and the request headers to be similar to what I got with chrome dev panel as you can see in my code.

Before i made it to work with Selenium library but it is not good for uses.

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)

Please help me to understand what is wrong here or is there another library that can do this except Selenium

Thank you in advance!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值