其实 Python 中一般都是直接用 urllib.urlopen() 来抓取网页内容或者模拟登陆等操作,但是 GAE 出于安全考虑不可以用 urlopen 操作,取而代之的就是 urlfetch.fetch()。fetch() 函数、参数(via):

fetch(url, payload=None, method=GET, headers={}, allow_truncated=False, follow_redirects=True, deadline=None)

Python语言: GAE urlfetch登录人人
01 def login_renren(self):
02     login_url = ‘http://passport.renren.com/PLogin.do’
03     login_data = urllib.urlencode(
04         {
05          ‘domain’:‘renren.com’,
06          ‘email’:  renren_username,
07          ‘password’: renren_passwd,
08          ‘origURL’:‘http://home.renren.com/Home.do’,
09          })
10     result = urlfetch.fetch(
11         url = login_url,
12         payload = login_data,
13         method = urlfetch.POST,
14         headers = {‘Cookie’:make_cookie_header(cookie),
15                    ‘Content-Type’:‘application/x-www-form-urlencoded’,
16                    ‘User-Agent’:‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6′ },
17         follow_redirects = False)

继续学习 GAE。

Tags: , , ,

Related posts

Tags: ,,,.
«
Home

No Comments Now!

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (required),(will not be published)
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://hanghang.name),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.