import requests
zend2 = 'https://zend2.com/'
visitSite = 'https://www.wikipedia.org/'
headers = {}
headers['User-Agent'] = 'Mozilla/5.0'
#zend2 data parameters
data_zend2 = {}
data_zend2['u'] = visitSite
#creating a session
mySession = requests.Session()
req_01 = mySession.get(zend2, headers = headers)
post_01 = mySession.post(zend2, data = data_zend2,headers = headers)
代码执行时没有错误,当我尝试访问维基百科时,它只是将我发送到错误的页面。我也尝试过使用 json.dumps() 来获取数据,但它不起作用。 如果还有其他我可以使用的库,我愿意接受建议。
最佳答案
您的帖子网址和数据不正确:
headers = {'User-Agent': 'Mozilla/5.0'}
# zend2 data parameters
data = {"u": "https://www.wikipedia.org/", "encodeURL": "on", "allowCookies": "on", "stripJS": "on",
"stripObjects": "on"}
with requests.Session() as s:
res = s.post("https://zend2.com/includes/process.php?action=update1", headers=headers, data=data)
print(res.text)
如果您在 Chrome 或 Firebug 中查看请求,在“其他”选项卡下您可以看到发生了什么:
如果我们使用 bs4 解析页面的一部分,您可以看到我们确实获取了返回的 wiki 页面内容:
我
n [15]: import requests
In [16]: from bs4 import BeautifulSoup
In [17]: headers = {'User-Agent': 'Mozilla/5.0'}
In [18]: data = {"u": "https://www.wikipedia.org/", "encodeURL": "on", "allowCookies": "on", "stripJS": "on",
....: "stripObjects": "on"}
In [19]: with requests.Session() as s:
....: res = s.post("https://zend2.com/includes/process.php?action=update1", headers=headers, data=data)
....: soup = BeautifulSoup(res.text,"html.parser")
....: prin(soup.select_one("div.central-featured-lang.lang1"))
....:
<div class="central-featured-lang lang1" lang="en">
<a class="link-box" href="/open12.php?u=DQYIaIkz%2BRfJh%2B3hqurO3R7xaOuC&b=29" title="English — Wikipedia — The Free Encyclopedia">
<strong>English</strong><br>
<em>The Free Encyclopedia</em><br>
<small>5 077 000+ articles</small>
</br></br></a>
</div>
关于Python 3.4 我正在尝试向匿名代理 zend2.com 发送发布请求。但我只是得到他们的主页作为回应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397769/