Atishay
Atishay

Reputation: 31

Python beautiful soup and requests submit form data on post url

How can i post form data on the url: http://washington.kdmid.ru/queue-en/Visitor.aspx

When i submit form data with below fields i am getting same page in response instead of redirect to next page.

import requests
from bs4 import BeautifulSoup

location_url = "http://washington.kdmid.ru/queue-en/visitor.aspx"
s = requests.Session()

main_page = s.get(location_url)

main_html = BeautifulSoup(main_page.text)


c_form = main_html.find_all("form")[0]
c_form_submit = c_form.attrs["action"]
data = {e.attrs.get("name"): e.attrs.get("value") for e in c_form.find_all("input")}

data["ctl00$MainContent$txtFam"] = "bsssabassra"
data["ctl00$MainContent$txtIm"] = "Akssssshassya"
data["ctl00$MainContent$txtOt"] = "a"
data["ctl00$MainContent$txtTel"] = "1122334455"
data["ctl00$MainContent$txtEmail"] = "[email protected]"
data["ctl00$MainContent$DDL_Day"] = 1
data["ctl00$MainContent$DDL_Month"] = 1
data["ctl00$MainContent$TextBox_Year"] = 1993
data["ctl00$MainContent$DDL_Mr"] = "MR"
data["ctl00$MainContent$txtCode"] = captcha_txt
data["ctl00$MainContent$ButtonA"] = "Next"
import json; json.dumps(data)
submit_captcha_resp = s.post("http://washington.kdmid.ru/queue-en/visitor.aspx", 
data=json.dumps(data))
final_page = BeautifulSoup(submit_captcha_resp.text)

Upvotes: 0

Views: 1851

Answers (2)

electromeow
electromeow

Reputation: 251

It wont redirect, because it's not a browser. BS don't run the JS scripts or HTML code. But you get the response. You should use one of these:

submit_captcha_resp = s.post("yourLongURL", json=data)

or

submit_captcha_resp = s.post("yourLongURL", data=data)

json.dumps() is used to convert a JSON to a string but you don't need that because the webpage which you are posting data uses HTML tag and form tag posts the data without converting it to string. So you shouldn't convert it to a string. You should post it in JSON format.

And as @dharmey said: If you get a 404, you should set a user agent as a popular web browser. For example:

{"User-Agent":"Mozilla/5.0"}

And I think now you have bigger problems like passing the Captcha.

Upvotes: 1

dharmey
dharmey

Reputation: 85

I think you might be posting the data in the wrong way. You could try

submit_captcha_resp = s.post("http://washington.kdmid.ru/queue-en/visitor.aspx", 
json=data)

Instead of data=json.dumps(data))

If this dosen't work / the site requires actual form data, try to pass in some headers, as they might be required for the server to recieve the request correctly.

You could just include

headers = {
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}

submit_captcha_resp = s.post("http://washington.kdmid.ru/queue-en/visitor.aspx", 
headers=headers, data=json.dumps(data))

to start out.

Upvotes: 0

Related Questions