snow_man_J 2015-03-17 07:32 采纳率: 100%
浏览 3708
已采纳

python模拟twitter登陆

这是我的代码,不知道为什么一直无法成功
import urllib2
import urllib
import cookielib
import re

class Fetcher(object):
def init(self,name=None,pwd=None):
self.cj = cookielib.LWPCookieJar() #获取一个保存cookie的对象。
self.cookie_processor = urllib2.HTTPCookieProcessor(self.cj) #将一个保存cookie对象,和一个HTTP的cookie的处理器绑定
self.opener = urllib2.build_opener(self.cookie_processor, urllib2.HTTPHandler) #创建一个opener,将保存了cookie的http处理器,设置一个handler用于处理http的URL的打开
urllib2.install_opener(self.opener) #将包含了cookie、http处理器、http的handler的资源和urllib2对象绑定在一起
self.name = name
self.pwd = pwd
self.hosturl = 'https://twitter.com/' #登录的主页面

self.posturl = 'https://twitter.com/sessions' #post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据)

def login(self):
    #跳转到登陆界面,此时可获得cookie
    req=urllib2.Request(self.hosturl)
    resp=urllib2.urlopen(req)
    html = resp.read()

    #得到表单中的authenticity_token
    att=re.compile('value="(.*?)" name="authenticity_token"')
    t = att.search(html)
    authenticity_token = t.group(1)

    #处理cookie
    #cookies = ''
    #for index, cookie in enumerate(self.cj): 
    #    cookies = cookies+cookie.name+"="+cookie.value+";";
    #cookie = cookies[:-1]
    #print "cookies:",cookie

    #request headers,仿冒浏览器
    self.header = {#':host': 'twitter.com',
              #':method': 'POST',
              #':path': '/sessions',
              #':scheme': 'https',
              #':version': 'HTTP/1.1',
              #'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
              #'accept-encoding': 'gzip,deflate,sdch',
              #'accept-language': 'zh-CN,zh;q=0.8',
    #          'cache-control': 'max-age=0',
    #          #'content-length': '214',
              #'content-type': 'application/x-www-form-urlencoded',
    #          'cookie': cookie,
              #'origin': 'https://twitter.com',
              'referer': 'https://twitter.com/',
              'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'
             }
    #print(self.cj)

    #form data,提交的表单数据
    postForm = {'session[username_or_email]':self.name,
                    'session[password]':self.pwd,
                    'remember_me':'1',
                    'return_to_ssl':'true',
                    'scribe_log':'',
                    'redirect_after_login':'/',
                    'authenticity_token':authenticity_token,
                    }
    postData=urllib.urlencode(postForm) #编码
    #print(postData)

    #提交表单
    rep = urllib2.Request(self.posturl,postForm,self.header)
    resp=urllib2.urlopen(req)
    html = resp.read()
    print(html)

    #req=urllib2.Request(self.hosturl)
    #resp=urllib2.urlopen(req)
    #html = resp.read()
    #print(html)

if name == '__main__':
username = '*******' #用户名
password = '******' #密码
fet = Fetcher(username,password);
fet.login()

大神帮忙看看吧。

我的思路和网上的思路一样的,我是个菜鸟,也没什么币,希望大家见谅了

  • 写回答

4条回答 默认 最新

  • onionschool 2015-03-17 10:13
    关注

    你可以试试 requests库。
    http://www.python-requests.org/en/latest/user/advanced/#session-objects
    做登录主要是状态同步的问题。这个库封装的比较好。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看