[英]Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,))
I don't understand why the connection aborted, I have included headers and cookies in my request.我不明白为什么连接中止,我在我的请求中包含了标题和 cookies。 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')",))
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.