VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python爬虫 >
  • python爬虫之tornado登陆豆瓣代码实例

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/pythonxin3721/


分享一段tornado登陆豆瓣的代码,只简单实现了登陆,并没有实现异步登陆。需要的网友可以自己将代码完善一下。

直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#coding=utf8
import settings
import urllib
from tornadoimport httpclient
import json
 
class douban:
    authurl= 'https://www.douban.com/service/auth2/'
    user_info_url= 'https://api.douban.com/v2/user/~me'
 
    def get_authorization_code(self):
        params= {
            "client_id":settings.oauth2['douban']['key'],
            "redirect_uri":settings.oauth2['redirect_url'],
            "response_type":"code",
            "scope":"douban_basic_common",
        }
        return self.authurl+'auth?'+urllib.urlencode(params)
 
    def get_access_token(self,code):
        params= {
            "client_id":settings.oauth2['douban']['key'],
            "client_secret":settings.oauth2['douban']['sercet'],
            "redirect_uri":settings.oauth2['redirect_url'],
            "grant_type":"authorization_code",
            "code":code,
        }
        url= self.authurl+'token'
        http_client= httpclient.HTTPClient()
        req= httpclient.HTTPRequest(url,method='POST',body=urllib.urlencode(params))
        response= http_client.fetch(req)
        return json.loads(response.body)
             
    def get_user_info(self,access_token):
        url= 'https://api.douban.com/v2/user/~me'
        http_client= httpclient.HTTPClient()
        req= httpclient.HTTPRequest(url,headers={"Authorization":"Bearer "+access_token})
        response= http_client.fetch(req)
        return json.loads(response.body)
相关教程