使用python填写在线表单;标准实施不起作用 [英] Using python to fill out an online form; standard implementation does not work

查看:191
本文介绍了使用python填写在线表单;标准实施不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.7.5来尝试登录到网站。我需要登录到此网站,然后导航到其他几个页面以从中提取表格。但现在,我的问题在于简单地登录到网站。登录页面如下所示:

 < form action =/ sessionclass =textmethod = >< div style =margin:0; padding:0; display:inline>< input name =authenticity_tokentype =hiddenvalue =xeSbOkcWd444xhHyLj82wLS62qfH72De + 7lwIhWFRd4 =/>< / DIV> < p为H. 
< label for =login>用户名< / label>< br />
< input id =loginname =logintype =text/>< br />
< label for =password>密码< / label>< br />
< input id =passwordname =passwordtype =password/>
< a href =/ forgot_password>(忘记密码?)< / a>
< / p>

< p>
< input id =remember_mename =remember_metype =checkboxvalue =1/>
< label class =shiftedlabelfor =remember_me>记住我< / label>
< / p>

< p>
< br />< input name =committype =submitvalue =登录/>
< / p>
< / form>

我在下面的代码中使用了cookiejar,urllib和urllib2,我从上一个问题,我稍微修改了一下:

 导入urllib,urllib2,cookielib 

用户名='namehere'
password ='passwordhere'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data ='urllib.urlencode({'Username':username,'password':password,'Remember_me':1,'commit':'登录'})
opener.open('http:// example .org / login.php',login_data)
resp = opener.open('http://example.org/password_protected_pa​​ge')
print resp.read()

我在原始的登录数据中添加了两个字段,记住我并提交。



当我运行这段代码时,我得到了一个传递措辞页面的打印输出,但它有错误,我必须登录才能看到此页面,并且看不到我需要的表格。请注意,网站上这个页面不存在.php扩展名,我不知道这个差别有多大。



在相关说明中,我发现这种类型的另一个最常见的解决方案是使用机械化模块。然而,我无法安装它用来安装自己的简易安装程序工具,因为我对此很新,所以我无法诊断问题。但是,这是一个单独的问题。



感谢您的帮助: p>我建议查看 Charles 程序。找到发送到服务器的数据是非常好的,通常使用之后的urllib模拟相同的请求非常简单。



在你的情况下,你看起来像不是' t将authenticity_token的值添加到您的POST中,名称记住我实际上是remember_me,并且名称用户名实际上是登录。


I'm using Python 2.7.5 to try and log in to a website. I need to log in to this site, and then navigate to several other pages to extract tables from them. For now though, my problem lies with simply logging in to the site. The for the login page looks like this:

<form action="/session" class="text" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="xeSbOkcWd444xhHyLj82wLS62qfH72De+7lwIhWFRd4=" /></div>    <p>
    <label for="login">Username</label><br />
    <input id="login" name="login" type="text" /><br />
    <label for="password">Password</label><br/>
    <input id="password" name="password" type="password" />
    <a href="/forgot_password">(Forgotten your password?)</a>
</p>

<p>
    <input id="remember_me" name="remember_me" type="checkbox" value="1" />
    <label class="shiftedlabel" for="remember_me">Remember me</label>
</p>

<p>
    <br /><input name="commit" type="submit" value="Log in" />
</p>
</form>

I have been using cookiejar, urllib and urllib2, in the following code, which I got from this previous question, which I have modified slightly below:

import urllib, urllib2, cookielib

username = 'namehere'
password = 'passwordhere'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'Username' : username, 'password' : password, 'Remember_me' : "1", 'commit' : 'Log in'})
opener.open('http://example.org/login.php', login_data)
resp = opener.open('http://example.org/password_protected_page')
print resp.read()

I have added two fields to the original "login data", remember me and submit.

When I run this code, I get a printout of the pass worded page, but it has the error that I must be logged in to see this page, and cannot see the table I need to. Please note that a .php extention does not exist for this page on the website, I don't know how much of a difference that makes though.

On a related note, the other most common solution I found for this type of thing was to use the mechanize module. I however was unable to install the "easy installer" tool it uses to install itself, and as I'm fairly new to this I wasn't able to diagnose the problem. That's a separate issue though.

Thanks for any help :)

解决方案

I'd suggest checking out the program Charles. It's great for finding the data that is sent to the server, it's generally pretty straightforward to emulate that same request with urllib afterwards.

In your case it looks like you aren't adding the value of authenticity_token to your POST, the name "Remember me" is actually "remember_me", and the name "Username" is actually "login".

这篇关于使用python填写在线表单;标准实施不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆