模拟登陆-python模拟twitter登陆

问题描述

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

class Fetcher(object):
def init(selfname=Nonepwd=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/htmlapplication/xhtml+xmlapplication/xml;q=0.9image/webp*/*;q=0.8'              #'accept-encoding': 'gzipdeflatesdch'              #'accept-language': 'zh-CNzh;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.posturlpostFormself.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(usernamepassword);
fet.login()

大神帮忙看看吧。

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

解决方案

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

解决方案二:
不怎么会发帖,大家将就看吧

解决方案三:
先看看返回的错误信息,比如是认证没过还是网络不通等

解决方案四:
已经调试好了
一个是
rep = urllib2.Request(self.posturlpostFormself.header)
resp=urllib2.urlopen(req)
参数写错了,还有就是第一句不能加header,估计header参数有问题,去掉header就好了。
谢谢大家了

时间: 2024-09-01 02:00:26

模拟登陆-python模拟twitter登陆的相关文章

网络爬虫-关于用python模拟登陆新浪微博返回内容

问题描述 关于用python模拟登陆新浪微博返回内容 第一次提交登陆信息和参数后,按照网上大神们的代码应该是在返回的内容中找到location.replace(....)提取url然后再访问,但是我post表单后得到的似乎不是登陆页面也不是正常登陆的页面 不知道这是一个什么页面...算是登陆上去了吗? 解决方案 基于python的新浪微博模拟登陆2014_新浪微博模拟登陆_python模拟新浪微博登陆---------------------- 解决方案二: 看上去是一个登录后的判断页面,要看你

python模拟登陆新浪微博,设置登录不需要验证码,代码返回验证码错误

问题描述 python模拟登陆新浪微博,设置登录不需要验证码,代码返回验证码错误 网页登陆不需要验证码 代码返回retcode=2070 验证码输入错误 求大神指点 解决方案 用fiddler调试下,看看是否带上了cookie,新浪是否传给你验证码,如果传了,必须先识别. 解决方案二: http://blog.csdn.net/ta790799213/article/details/44205351http://www.crazyant.net/796.htmlhttp://www.2cto.c

python模拟登陆csdn-python模拟csdn的登录页面,可是登录失败,只是返回登陆页面的代码

问题描述 python模拟csdn的登录页面,可是登录失败,只是返回登陆页面的代码 #!/usr/bin/python#encoding=utf-8import HTMLParserimport urlparseimport urllibimport urllib2import cookielibimport stringimport re hosturl = 'http://my.csdn.net'posturl = 'http://passport.csdn.net/account/logi

python'模拟登陆网站一直返回登陆界面

问题描述 python'模拟登陆网站一直返回登陆界面 import urllib.request import urllib.parse import http.cookiejar def gethtml(url): cj = http.cookiejar.CookieJar() cookie_jar = urllib.request.HTTPCookieProcessor(cj) opener = urllib.request.build_opener(cookie_jar) urllib.r

爬虫-用python模拟登陆csdn时参数lt要如何获得?

问题描述 用python模拟登陆csdn时参数lt要如何获得? 网上看到代码是用正则表达式,可是我打印出来的网页代码里没有相关的内容,运行 代码用findall返回的也是一个空列表..请问应该在哪里得到lt参数? 解决方案 可能是js运行时动态生成. 解决方案二: 你把你登录时候的lt参数值复制下来,填充到你的程序里面试试.

python模拟登陆后,下载网站图片

问题描述 python模拟登陆后,下载网站图片 采用的 urllib2 cookielib 两个包 模拟Cookie及登陆网站,登陆成功后,也筛选出了图片地址,采用urllib2.urlopen打开图片,再写到本地,图片是损坏的,图片打开是花的,怎么使用urllib.urlretrieve下载图片?将urllib2 的cookie值付给urllib.urlretrieve?不然提示无权限下载?高手帮忙 解决方案 根据Need urllib.urlretrieve and urllib2.Open

python模拟登陆获取和处理发送post request和head数据

今天这篇文章说下如何获取和处理发送post request和head数据. 工具: firefox浏览器及firebug插件. (其他的如httpfox,live http head   ,fiddler,httpwatch  也行) 1.查看分析登陆页面html代码,看是否有iframe 我们写一个自动登录的脚本的时候,先要分析出需要post request和head数据,以及post的网址等.这里,我们先打开firebug开始监控,然后打开网站的登陆页面:/IndexPage/Index.a

python模拟登陆之验证码与cookies的同步处理思路

自动登陆可能是写爬虫的第一步,如果都不能登陆,很多东西爬不到的.这也不是第一次写包含验证码识别的自动登陆脚本了.这次有点被坑住了,把这次的记录下来. 这次要自动登陆的网站地址是:2013年株洲市中小学教师全员培训   /IndexPage/Index.aspx 先说下思路,好多人写那些不需要验证码识别的自动登陆脚本很容易,只要保存好cookies就可以了,但是对于需要验证码的网站就总是登陆不上去. 对于需要验证码的网站的自动登陆脚本的步骤:(以上面我说的那个网站为例,对于python和其他语言,

python模拟新浪微博登陆功能(新浪微博爬虫)_python

1.主函数(WeiboMain.py): 复制代码 代码如下: import urllib2import cookielib import WeiboEncodeimport WeiboSearch if __name__ == '__main__':    weiboLogin = WeiboLogin('×××@gmail.com', '××××')#邮箱(账号).密码    if weiboLogin.Login() == True:        print "登陆成功!" 前