主要原理:
利用python的urllib2库来发送post数据(包括账号密码等)
要传送哪些post数据通过firefox+httpfox来查看
最后用re来提取获取的验证码
代码如下:
- #!/usr/bin/python
-
-
import re
-
import urllib, urllib2
-
-
from weibopy.auth import OAuthHandler
-
from weibopy.api import API
-
-
AppKey = '***'
-
AppSecret = '***'
-
my_auth = OAuthHandler(AppKey , AppSecret)
-
-
postdata = urllib.urlencode({
- 'userId':'***',
- 'passwd':'***',
- 'action':'submit',
- })
-
req = urllib2.Request(
- url = my_auth.get_authorization_url(),
- data = postdata
- )
-
result = urllib2.urlopen(req).read()
-
patternpim = re.compile("(\d{6})")
-
-
try:
- pim = patternpim.findall(result)[0]
-
except:
- print "Log in failed!"
- print "Input correct username and password please!"
- exit(1)
- print "Log in success!"
-
-
my_auth.get_access_token(pim)
-
my_api = API(my_auth)
-
while 1:
- query_cmd = raw_input('1(Get News) 2(Exit): ')
- if query_cmd == '1':
- for friend in my_api.friends_timeline():
- object = friend
- user = object.__getattribute__("user")
- text = object.__getattribute__("text")
- print "[" + user.name + "]" + " : " + text
- elif query_cmd == '2':
- exit(0)
阅读(1863) | 评论(0) | 转发(0) |