Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1121553
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: LINUX

2011-06-18 16:58:55

主要原理:
利用python的urllib2库来发送post数据(包括账号密码等)
要传送哪些post数据通过firefox+httpfox来查看
最后用re来提取获取的验证码

代码如下:
  1. #!/usr/bin/python
  2. import re
  3. import urllib, urllib2
  4. from weibopy.auth import OAuthHandler
  5. from weibopy.api import API
  6. AppKey = '***'
  7. AppSecret = '***'
  8. my_auth = OAuthHandler(AppKey , AppSecret)
  9. postdata = urllib.urlencode({
  10. 'userId':'***',
  11. 'passwd':'***',
  12. 'action':'submit',
  13. })
  14. req = urllib2.Request(
  15. url = my_auth.get_authorization_url(),
  16. data = postdata
  17. )
  18. result = urllib2.urlopen(req).read()
  19. patternpim = re.compile("(\d{6})")
  20. try:
  21. pim = patternpim.findall(result)[0]
  22. except:
  23. print "Log in failed!"
  24. print "Input correct username and password please!"
  25. exit(1)
  26. print "Log in success!"
  27. my_auth.get_access_token(pim)
  28. my_api = API(my_auth)
  29. while 1:
  30. query_cmd = raw_input('1(Get News) 2(Exit): ')
  31. if query_cmd == '1':
  32. for friend in my_api.friends_timeline():
  33. object = friend
  34. user = object.__getattribute__("user")
  35. text = object.__getattribute__("text")
  36. print "[" + user.name + "]" + " : " + text
  37. elif query_cmd == '2':
  38. exit(0)
阅读(1825) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~