1

这是我的带有 Curl 的 PHP 代码。我需要在 GAE Python 中使用 urlfetch 执行相同的功能。如何将所有这些参数传递给 urlfetch。请帮我。

    $curl = curl_init();
    $timeout = 30;

// Logining to my TNT 
    curl_setopt ($curl, CURLOPT_URL, "https://my.tnt.com/myTNT/login/LoginInitial.do?cmd=1&navigation=1");
    curl_setopt ($curl, CURLOPT_POST, 1);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, "userid=aaaa@bb.com&password=1234qwe");
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt ($curl, CURLOPT_COOKIESESSION, 1);
    curl_setopt ($curl, CURLOPT_COOKIEFILE, "userid=; password=; JSESSIONID=E1FC9A6D18002370BD4AF7DDBBA617A0; BIGipServermy_tnt_com_pool=2636720036.20480.0000");
    curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($curl, CURLOPT_MAXREDIRS, 20);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");
    curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt ($curl, CURLOPT_REFERER, "https://my.tnt.com/myTNT/login/LoginInitial.do");
    $text = curl_exec($curl);
    $pos = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);

这是我的python代码。

from google.appengine.api import urlfetch
import urllib

class MainHandler(webapp.RequestHandler):
  def get(self):
     url = "https://my.tnt.com/myTNT/login/LoginInitial.do?cmd=1&navigation=1"
     form_fields = {
         "userid": "aaaa@bb.com",
         "password": "1234qwe",
     }
     form_data = urllib.urlencode(form_fields)
     result = urlfetch.fetch(url=url,
                    payload=form_data,                        
                    method=urlfetch.POST,
                    validate_certificate='TRUE',
                    headers={'Host': 'my.tnt.com',
                                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                                    'Accept-Language': 'en-us,en;q=0.5',
                                    'Accept-Encoding': 'gzip, deflate',
                                    'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                                    'Keep-Alive': '115',
                                    'Connection': 'keep-alive',
                                    'Referer': 'https://my.tnt.com/myTNT/login/LoginInitial.do',
                                    'Cookie': 'userid=; password=; JSESSIONID=E1FC9A6D18002370BD4AF7DDBBA617A0; BIGipServermy_tnt_com_pool=2636720036.20480.0000',
                                    'Content-Type': 'application/x-www-form-urlencoded',
                                    'Content-Length': '45',
                                   }
                      )
    self.response.out.write(result.final_url)

我正在尝试访问 MyTNT 网页。所以首先我需要登录到那个页面。以上代码用于登录 mytnt 网站。当我运行 PHP 代码时,它将重定向到 mytnt 主页(https://my.tnt.com/myTNT/landing/landingPage.do)。但是当我运行 python 文件时,它被重定向到同一个登录页面。当我使用 urlfetch 执行 python 文件时,登录不成功。

4

1 回答 1

0

我怀疑您的 php 代码正在使用类似于mechanize库中可用的“cookie jar”。

我以前使用 mechanize 进行网站的基本抓取,但实际上并没有登录,所以我不能说它 100% 对你有用,但我认为这是你最好的选择。

于 2011-08-23T13:29:28.880 回答