这个python脚本正确吗?


import urllib, urllib2, cookielib 

username = 'myuser' 
password = 'mypassword' 

cj = cookielib.CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
login_data = urllib.urlencode({'username' : username, 'j_password' : password}) 
opener.open('http://www.example.com/login.php', login_data) 
resp = opener.open('http://www.example.com/hiddenpage.php') 
resp.read()

我找到了这个脚本 这里它是要先登录网页,检索cookie,存储并使用它们以在同一网站中打开其他页面。我想以这种方式登录到我的eBay帐户(URL是 https://signin.ebay.com/ws/ebayisapi.dll?signin )然后在我的eBay帐户上转到我的收件箱(URL为 http://my.ebay.com/ws/ebayisapi.dll?myebay&gbh=1) .

因此,以下是我在此脚本中需要使用的值:

首先(sing-in)URL: https://signin.ebay.com/ws/ebayisapi.dll?signin

第二URL: http://my.ebay.com/ws/ebayisapi.dll?myebay&gbh=1

我在eBay上的登录名: tryinghard

我在eBay上的密码: gettingsomewhere

使用所有这些新值,上面的脚本必须以这种方式看起来:


import urllib, urllib2, cookielib 

username = 'tryinghard' 
password = 'gettingsomewhere' 

cj = cookielib.CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
login_data = urllib.urlencode({'username' : username, 'j_password' : password}) 
opener.open(https://signin.ebay.com/ws/eBayISAPI.dll?SignIn', login_data) 
resp = opener.open(http://my.ebay.com/ws/eBayISAPI.dll?MyEbay&gbh=1') 
resp.read()

这是正确的吗?我对 login_data = 线(从底部发出第四个),为什么 j_password 而不是只是 password?

我尝试了所有这些值的脚本,但它不起作用。有人知道为什么在我的情况下它不起作用吗?

我已经 学会了如何登录我的eBay帐户,然后通过运行使用Twill作为外部模块的Python脚本检查那里的其他一些页面, ,但这只有当我从命令提示符或python shell运行该脚本时才成功。当我尝试通过 “ Google App Engine软件开发套件” 我从 “ Google App Engine”.

后来我被告知 这里 这没有成功,因为“ Google App Engine”不喜欢外部模块。这就是为什么我找到这个脚本的原因 - 一开始它正在导入的那些模块(Urllib, Urllib2, 库克里卜)都是内置模块。

有帮助吗?

解决方案

登录页面上的一个简单的“查看源”,您提供的URL很容易显示以下有关它的细节...(只需将HTML的格式化最小化以获取可读性):

<span style="display:-moz-inline-stack" class="unl">
  <label for="userid">User ID  </label></span>
<span><input size="27" maxlength="64" class="txtBxF"
       value="" name="userid" id="userid"></span></div>
<div><span style="display:-moz-inline-stack" class="unl">
  <label for="pass">Password  </label></span>
<span><input size="27" maxlength="64" class="txtBxF"
       value="" name="pass" id="pass" type="password"></span>

正如您一眼看到的那样,关键输入字段的名称是 不是 usernamej_password 当您使用的是 useridpass. 。因此,您的代码显然无法按照目前的影响。

阅读更多页面,您也很快就会看到:

<input type="checkbox" name="keepMeSignInOption" value="1" id="signed_in"></b>
<span class="pcsm"><label for="signed_in"><b>Keep me signed in for today.</b>

最有可能您必须模拟该复选框以获取可用的cookie(至少除了短暂的时间以外的任何东西;-)。

依此类推,依此类推 - 尝试与页面进行自动互动的尝试而不费心阅读该页面的源以获取实际的ID和名称来使用Strikike Iss Iss Iss Iss Iss Iss Iss Interse,以表现出对生活的非常乐观的态度,宇宙,宇宙,和所有...;-)。顺便说一下,简化了这种互动( 仔细阅读来源;-),我发现 机械化 非常方便(比试图砍掉它更强大 只是 与标准库一样,就像您所做的那样)。

另外,在与网站自动互动之前,请务必查看其 robots.txt 为了确保您不会打破其使用条款 - 网站可以轻松地识别“机器人”(自动互动)而不是“人类”,并通过禁止,黑名单和更糟的情况来反对机器人。您真的不想遇到;-)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top