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

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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable