Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要requests.get一个cookie(ASP.NET_SessionID),然后requests.post,cookie作为参数_Python_Asp.net_Post_Python Requests_Sessionid - Fatal编程技术网

Python 需要requests.get一个cookie(ASP.NET_SessionID),然后requests.post,cookie作为参数

Python 需要requests.get一个cookie(ASP.NET_SessionID),然后requests.post,cookie作为参数,python,asp.net,post,python-requests,sessionid,Python,Asp.net,Post,Python Requests,Sessionid,我在获取cookie、将其传递到参数列表,然后使用requests lib发布cookie时遇到问题 我用Burpsuite截取了帖子,sessionId是参数之一,请参见下面的屏幕截图。 网页的源代码在下面的屏幕截图中 我的代码包括在下面: import requests import cookielib import sys from bs4 import BeautifulSoup print "Enter the url", url = raw_input print url r

我在获取cookie、将其传递到参数列表,然后使用requests lib发布cookie时遇到问题

我用Burpsuite截取了帖子,sessionId是参数之一,请参见下面的屏幕截图。

网页的源代码在下面的屏幕截图中

我的代码包括在下面:

import requests
import cookielib
import sys
from bs4 import BeautifulSoup

print "Enter the url",
url = raw_input
print url
r = requests.get(url)
c = r.content
soup = BeautifulSoup(c)

#Finding Captcha
div1 = soup.find('div', id='Custom')
comment = next(div1.children)
captcha = comment.partition(':')[-1].strip()
print captcha

#Finding viewstate
viewstate = soup.findAll("input", {"type" : "hidden", "name" : "__VIEWSTATE"})
v = viewstate[0]['value']
print v

#Finding eventvalidation
eventval = soup.findAll("input", {"type" : "hidden", "name" : "__EVENTVALIDATION"})
e = eventval[0]['value']
print e

# Get Cookie (this is where I am confused), and yes I have read through the Requests and BS docs
s = r.cookies
print s # Prints the class call but I don't get anything I can pass as a parameter

#Setting Parameters
params = { 
          '__VIEWSTATE' : v,
          'txtMessage'  : 'test',
          'txtCaptcha'  : captcha,
          'cmdSubmit'   : 'Submit',
          '__EVENTVALIDATION' : e
          #Need ASP.NET_SessionId Key : Value here
}

#Posting
requests.post(url, data=params)

print r.status_code
所以说得很清楚,当我连接到web服务器时,我试图获取sessionId,并将其作为参数发布到此留言板。这是针对沙盒虚拟机上的实验室,而不是实时站点。这是我第一次用Python写文章,所以如果我有错误,我已经尽我所能阅读了Lib文档和其他网站

谢谢

将“s”作为参数传递给您的帖子

s = r.cookies
print s # Prints the class call but I don't get anything I can pass as a parameter
您需要将cookies作为名为“cookies”的参数传递。在中的源代码中,它表示cookie可以是CookieJar,也可以是包含您想要传递的cookie的字典

在您的情况下,只需将您的cookie复制到下一篇文章就更容易了,无需将它们转换为字典

设置参数 但是,我强烈建议您使用requests.Session()对象


这与我正在编写的代码相比会是什么样子?我是否调用会话对象并通过该对象运行get和post,以便所有cookie和参数保持持久性?我正在学习请求库。是的,基本上不是调用requests.get(),而是先创建一个会话。session=requests.session(),然后调用与试图调用的方法相同的方法,但调用的是会话对象。会话只跟踪cookie,而不跟踪参数。你将需要改变参数,因为你希望为每一篇文章,并得到你想要的。非常感谢让我尝试一下,我会圈回来。
params = { 
         '__VIEWSTATE' : v,
         'txtMessage'  : 'test',
         'txtCaptcha'  : captcha,
         'cmdSubmit'   : 'Submit',
          '__EVENTVALIDATION' : e
          #Need ASP.NET_SessionId Key : Value here
}

#Posting
requests.post(url, data=params,cookies=s)
session = requests.Session()
session.get(url)
session.get(url2)
#It will keep track of your cookies automatically for you, for every domain you use your session on . Very handy in deed, I rarely use requests.get unless I don't care at all about cookies.