python3.4我正在尝试向一个匿名代理zend2.com发送post请求。但我只是得到他们的主页作为回应

2024-06-14 23:09:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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文件()的数据,但它没有工作。 如果有其他的图书馆我可以使用我是开放的建议。你知道吗


Tags: httpsorgimportcomdatawwwwikipediapost
1条回答
网友
1楼 · 发布于 2024-06-14 23:09:15

您的帖子url和数据不正确:

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中查看请求,在其他选项卡下可以看到发生了什么:

enter image description here

如果我们使用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&amp;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>

相关问题 更多 >