Python:Mechanize 无法将 Cookie 发送到第二个 URL
我在尝试让 Mechanize 在请求第二个 URL 时保留第一个请求的 URL 设置的 cookie 时遇到严重问题。为了测试是否发送了任何内容,我将以下文件放在我的服务器上(cookies.php):
<?php
echo "cookies:::";
print_r($_COOKIE);
?>
以下是 python 文件,这意味着实现我的目标。我希望第一个站点设置的 cookie(我输出用于测试)也由我的 PHP 文件输出(又名在第二个请求之前设置):
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import mechanize
from pprint import *
#
class NullCookieProcessor(mechanize.HTTPCookieProcessor):
def http_request(self, request): return request
def http_response(self, request, response): return response
opener = mechanize.build_opener(NullCookieProcessor)
request = mechanize.Request('http://www.google.com')
response = mechanize.urlopen(request)
cj = mechanize.CookieJar()
cj.extract_cookies(response, request)
pprint(cj)
request2 = mechanize.Request('http://[domain2].com/cookies.php')
cj.add_cookie_header(request2)
response2 = mechanize.urlopen(request2)
print response2.geturl()
print response2.info() # headers
print response2.read() # body (readline and readlines work too)
以下是输出:
<mechanize._clientcookie.CookieJar[Cookie(version=0, name='NID', value='50=rkj1MMbufL7KRMj00TMF4rI4x7VNYgzWk5P97V05gBAMVOrYuSbb6-hpXVC3y_eD999uECgnBn7YqZ-ZGB1kmWhc_wQWV9nKlPER4_3BWEVSGU632vXEhgYROAz3QrP5', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1331337059, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=20342e7c6a6b8f8b:FF=0:TM=1315525859:LM=1315525859:S=RppxtfAGwVsGkZiJ', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1378597859, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
http://[MY DOMAIN].com/cookies.php
Date: Thu, 08 Sep 2011 23:51:01 GMT
Server: Apache mod_fcgid/2.3.6 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
cookies:::Array
(
)
任何想法如何获取这能工作吗?应该注意的是,上面的代码是从文档复制+粘贴的...,并且我还尝试了文档提供的其他示例代码,可以在这里找到: http://wwwsearch.sourceforge.net/mechanize/doc.html
I'm having serious issues trying to get Mechanize to preserve the cookies set by the first requested URL for when requesting the second URL. In order to test if anything was being sent, I put the following file on my server (cookies.php):
<?php
echo "cookies:::";
print_r($_COOKIE);
?>
The following is the python file which means to accomplish my goal. I'd like for the cookies set by the first site, which I output for testing, to be outputted by my PHP file as well (aka to be set before the second request):
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import mechanize
from pprint import *
#
class NullCookieProcessor(mechanize.HTTPCookieProcessor):
def http_request(self, request): return request
def http_response(self, request, response): return response
opener = mechanize.build_opener(NullCookieProcessor)
request = mechanize.Request('http://www.google.com')
response = mechanize.urlopen(request)
cj = mechanize.CookieJar()
cj.extract_cookies(response, request)
pprint(cj)
request2 = mechanize.Request('http://[domain2].com/cookies.php')
cj.add_cookie_header(request2)
response2 = mechanize.urlopen(request2)
print response2.geturl()
print response2.info() # headers
print response2.read() # body (readline and readlines work too)
The following is the output:
<mechanize._clientcookie.CookieJar[Cookie(version=0, name='NID', value='50=rkj1MMbufL7KRMj00TMF4rI4x7VNYgzWk5P97V05gBAMVOrYuSbb6-hpXVC3y_eD999uECgnBn7YqZ-ZGB1kmWhc_wQWV9nKlPER4_3BWEVSGU632vXEhgYROAz3QrP5', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1331337059, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=20342e7c6a6b8f8b:FF=0:TM=1315525859:LM=1315525859:S=RppxtfAGwVsGkZiJ', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1378597859, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
http://[MY DOMAIN].com/cookies.php
Date: Thu, 08 Sep 2011 23:51:01 GMT
Server: Apache mod_fcgid/2.3.6 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
cookies:::Array
(
)
ANY ideas how to get this to work? It should be noted that the above code is copy+pasted from the docs..., and that I've also tried the other sample code provided by the documentation, which can be found here: http://wwwsearch.sourceforge.net/mechanize/doc.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
request1
和request2
来自不同的域。来自google.com
的 Cookie 不会发送到'[domain2].com'
,这是可以理解的。request1
andrequest2
are from different domains. It is understandable that cookies fromgoogle.com
are not sent to'[domain2].com'
.