谷歌应用引擎,Python:如何从登录页面获取参数? [英] Googles App Engine, Python: How to get parameters from a log-in pages?
问题描述
以下是此处:
所以简而言之,您需要查看登录
页面,查看它使用哪个params,例如
登录= xxx,密码= yyy,将其发布到
页面,并且您还必须管理
这些cookie,即图书馆
像斜纹等等。 p>
我怎样才能使用Python和Google App Engine来做到这一点?任何人都可以给我一些线索吗?我已经提出了一个关于认证请求的问题,但在这里看起来问题不同,因为我建议您查看登录页面并获取参数,并且还必须处理cookie。
有两种方法: 使用斜纹或机械化,斜纹只是一个简单的包装机械化你可能只是使用机械化( http:// wwwsearch .sourceforge.net / mechanize / ),但要使用机械化您可能需要做一些黑客行为,请参阅将机械化模块导入到python脚本以获取更多详细信息 b让我们看看如何登录雅虎
a)查看页面( https://login.yahoo.com/config/login_verify2?&.src=ym ),看看表单是什么样的,你可以用firebug检查,而不是寻找原始的HTML。
$ b b b)表单有登录和passwd两个字段,再加上一些更隐藏的字段让我们现在忽略它们,所以到现在为止我们有
form action url = https://login.yahoo.com/config/login?
form_data = {'login':'my_login','passwd':'my_passwd'}
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2。 HTTPCookieProcessor(jar))
form_data = urllib.urlencode(form_data)
从这个页面返回的数据包含重定向
resp = opener.open(url,form_data)
$ c)现在,来自雅虎的网页重定向到其他网页,例如:c $ c>
resp = opener。打开('http://mail.yahoo.com')
打印resp.read()
如果您看到打印输出,则说:xxxx |注销,嗯...您的浏览器不受官方支持。这意味着它已经登录了:)但是由于yahoo邮件是ajax页面,并且不支持我的简单脚本浏览器,所以我们可以通过欺骗浏览器类型来获得这个工具,并且可以做很多事情。
这里是最终的代码
import urllib,urllib2,cookielib
url =https://login.yahoo.com/config/login?
form_data = {'login':'your-login','passwd':'your-pass'}
jar = cookielib.CookieJar()
opener = urllib2。 build_opener(urllib2.HTTPCookieProcessor(jar))
form_data = urllib.urlencode(form_data)
从这个页面返回的数据包含重定向
resp = opener.open(url,form_data)
#雅虎重定向到http://my.yahoo.com,所以让我们去那里insetad
resp = opener.open('http://mail.yahoo.com')
print resp.read ()
您应该查看mechanzie代码或链接,如 http://www.voidspace.org.uk/cgi-bin/voidspace/downman .py?file = cookielib_example.py 来查看他们是如何做到的。
我们可以发布这些数据
Here is a quote from here:
So in short ... you need to look into login page, see what params it uses e.g login=xxx, password=yyy, post it to that page and you will have to manage the cookies too, that is where library like twill etc come into picture.
How could I do it using Python and Google App Engine? Can anybody please give me some clue? I have already asked a question about the authenticated request, but here it seems the matter is different as here I am suggested to look into login page and get parameters, and also I have to deal with cookies.
There are two ways
AS I told you use twill or mechanize, as twill is just a simple wrapper over mechanize you may just use mechanize(http://wwwsearch.sourceforge.net/mechanize/), but to use mechanize you may need to do some hacking see import mechanize module to python script for more details
Do it the hard way and learn something while doing that Lets see how to login to yahoo
a) look into the page (https://login.yahoo.com/config/login_verify2?&.src=ym) and see what does form look like, you can firebug to inspect instead of looking into raw html.
b) form has login and passwd two field, Plus some more hidden fields lets ignore them for now, so till now we have form action url= "https://login.yahoo.com/config/login?" form_data = {'login' : 'my_login', 'passwd' : 'my_passwd'}
c) we can post above data to the correct post url, and it may work but usually we will need to go to other pages and if we do not have cookie it will ask again for login. so lets use a cookie jar e.g.
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
form_data = urllib.urlencode(form_data)
# data returned from this pages contains redirection
resp = opener.open(url, form_data)
d) now the page from yahoo, redirects to other pages, e.g. if I want to see mail page, i will now go to that and cookies will take care of authentication e.g.
resp = opener.open('http://mail.yahoo.com')
print resp.read()
If you see printout it says , "xxxx| logout , Hmm... your browser is not officially supported." that means it has logged me in :), but as yahoo mail is a ajax page and doesn't support my simple scripting browser, we can get past this tool by spoofing browser type, and can do lots of stuff.
Here is the final code
import urllib, urllib2, cookielib
url = "https://login.yahoo.com/config/login?"
form_data = {'login' : 'your-login', 'passwd' : 'your-pass'}
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
form_data = urllib.urlencode(form_data)
# data returned from this pages contains redirection
resp = opener.open(url, form_data)
# yahoo redirects to http://my.yahoo.com, so lets go there insetad
resp = opener.open('http://mail.yahoo.com')
print resp.read()
You should look into mechanzie code or links like this http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=cookielib_example.py to see how they do it.
we can post this data
这篇关于谷歌应用引擎,Python:如何从登录页面获取参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!