使用python的请求网页(错误:请求主机名无效)

问题描述 投票:0回答:1

听是我的JavaScript代码,可用来从页面访问响应

let host = "<host-url>",
    frompath= "<url of main page from which request is initiated>",
    topath = "<url of the response page>.aspx";
    history.pushState({page: 1}, "title", host+frompath)

    xhttp=new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    text = xhttp.responseText;
    document.write(text);
    }
    };
    xhttp.open("GET",host+topath, true);
    xhttp.send();

我用python写的同一脚本

import requests

host = "<host-url>"
frompath= "<url of main page from which request is initiated>"
topath = "<url of the response page>.aspx"


headers = {
        'Host':host,
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Referer':host,
        'Origin':host+frompath
}

s = requests.Session()

response = s.post(host+topath,headers=headers)

print(response.text)

此代码运行正常,但作为响应,我得到了一个包含以下内容的错误页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>

在JavaScript中,我没有得到这个错误,但是在python中,我得到了这个错误...。

请帮助我...

javascript python python-3.x python-requests urllib
1个回答
0
投票

我已通过替换解决此问题

© www.soinside.com 2019 - 2024. All rights reserved.