0

I don't understand why the connection aborted, I have included headers and cookies in my request. Any input would be highly appreciated.

import requests


url = 'https://fp.trafikverket.se/boka/#/search/SPHhISIPAfhPP/5/0/0/0'
s = requests.session()
res = s.get(url)
cookies = dict(res.cookies)
headers = dict(res.headers)
headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
r = s.post(url, 
    cookies=cookies, headers=headers).text

>>> requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(54, 'ECONNRESET')",))
2
  • what data you're trying to post to the URL, indeed? Commented Jun 4, 2020 at 11:37
  • I’m not posting anything at the moment since I can’t establish a connection, but I will post a payload to fill the form that is on the site Commented Jun 4, 2020 at 11:42

1 Answer 1

1

Try the below code, it works.

import requests
import json

url = 'https://fp.trafikverket.se/boka/#/search/SPHhISIPAfhPP/5/0/0/0'
s = requests.session()
res = s.get(url)
cookies = dict(res.cookies)
headers = dict(res.headers)
headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
data = {}
r = s.post(url, data = json.dumps(data), cookies = cookies, headers = headers).text
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the input but it does not work, it says (is swedish) multiple error messeges in the html. As: Your browser is not supported, Cookies in your browser has to be activated, Third party cookies has to be activated, Your browser can not be set to private mode. It does not return the same html as when visiting the site in the browser directly.
Maybe you're looking at the bottom of the page, please scroll up and it's exactly similar to the page. I just inspect (ctrl+u) and compare them, responses are equal. Can you try print(r)
I believe it's the information from https://fp.trafikverket.se/boka/occasion-bundles that I'm after.
I have inspected, and the form that is supposed to be filled is not present in the response, for example the div class form-group is not present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.