PHP Curl 代码到 python 中的 urlfetch - 需要帮助

发布于 2024-11-30 16:06:50 字数 3247 浏览 3 评论 0原文

这是我使用 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, "[email protected]&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": "[email protected]",
         "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 文件时,登录不成功。

This is my PHP Code with Curl . I need to do the same function using urlfetch in GAE Python. How can pass all these parameter to urlfetch. Please help me.

    $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, "[email protected]&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);

This is my python Code.

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": "[email protected]",
         "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)

I'm trying to access the MyTNT webpage. So first i need to login to that page. Above code is for logging into the mytnt website. When i run the PHP Code, it will redirecting to the mytnt home page (https://my.tnt.com/myTNT/landing/landingPage.do).
But When i run the python file it was redirecting to the same login page. The Login was unsuccessful when i execute the python file using urlfetch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-12-07 16:06:50

我怀疑您的 php 代码使用的是“cookie jar”,就像 mechanize 库中提供的那样。

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

I suspect that your php code is using a "cookie jar" like what is available in the mechanize library.

I've used mechanize before for basic scraping of websites, but not actually to log in, so I can't say 100% that it will work for you but I think it's your best shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文