我刚开始学习Python,我是一个专家。我的专长是VBA。在
当涉及到从web上提取数据时,如果您是通过VBA进行的,那么winHttp是最好的方法。但是,使用VBA,您只能使用单线程。解决这个问题的一种方法是使用VBScript。虽然使用VBScript是实现这一点的最简单方法,因为VBA和VBS几乎完全相同,但从我读到的内容中,Python似乎是最好的语言。在
我在下面提供了两个示例,一个用VBA编写(工作),另一个用Python编写(不工作)。在
VBA
Dim postData As String
Dim myHttp2 As WinHttp.WinHttpRequest
'postData is the form data sent in the request body that contains a users login credentials
postData = "SMNC-ISO&LOC=US&target=HTTPS%3AIntranetSite.net&postpreservationdata=&USER=usr123&PASSWORD=pwd123"
set myHttp2 = CreateObject("winHTTP.WinHTTPrequest.5.1")
myHttp2.Open "GET", "https://login.someintranetsite.net", true
myHttp2.setRequestHeader "Request", "GET /abcd123/4567 HTTP/1.1"
myHttp2.setRequestHeader "Accept", "stuff"
myHttp2.setRequestHeader "Accept-Language", "en-US"
myHttp2.setRequestHeader "User-Agent", "stuff"
myHttp2.setRequestHeader "Accept-Encoding", "stuff"
myHttp2.setRequestHeader "Host", "login.someintranetsite.net"
myHttp2.send postData
myHttp2.WaitForResponse
Debug.Print myHttp2.responseText
'obtains the session cookie needed for other requests not shown here
cookie = myHttp2.getResponseHeader("Set-Cookie")
以上结果将导致来自服务器的有效响应。。但是使用Python。。。在
Python
^{pr2}$当我尝试运行上述python时,我得到以下结果:
^{3}$我一直在寻找解决这个问题的办法,尝试了几种不同的方法,但都没有成功。我做错什么了?(顺便说一句,以上是在Spyder/IPython中完成的)
额外问题。。如何在Python中获得会话cookie?非常感谢您的帮助!谢谢!在
该问题应该是由代理身份验证失败引起的。在
有两种解决方法:
如果是基本身份验证,请将代理传递给请求.get/post如下所示:
关于如何对其他方法进行身份验证,可以参考The Official Website of Python Requests。在
代码如下:
^{pr2}$相关问题 更多 >
编程相关推荐