zoukankan      html  css  js  c++  java
  • 模拟登陆

    环境: python scrapy

    乐8账号充值

    return [Request("http://gh.le890.com/checkcode.php", 
    meta = {'cookiejar' : response.meta['cookiejar']}, 
    callback = self.save_ver_code)
    ]
    ···
    def save_ver_code(self, response): 
    ver_code = 'code' + str(self._id)
    path = '/www/gh/crawler/ver_code/' + ver_code
    with open(path, 'wb') as f:
    f.write(response.body)
    ···
    print u'输入验证码:'
    code = raw_input()
    ···
    
    
    return [Request("http://tcoin.52tt.com/tcoin/login.shtml", 
    meta = {'cookiejar' : 1}, 
    callback = self.post_login)
    ]
    ···
    def post_login(self, response):
    # asprint 'Preparing login'
    self._csrf = response.xpath('//input[@name="_csrf"]/@value').extract()[0]
    # 提交表单
    return [FormRequest("http://tcoin.52tt.com/tcoin/login",
    meta = {'cookiejar' : response.meta['cookiejar']},
    headers = self.headers,
    formdata = {
    'username': 'username',
    'password': 'password',
    'submit': '',
    '_csrf': self._csrf
    },
    callback = self.read_account_message,
    dont_filter = True
    )] 
    
    • 保存session:(乐8平台需要将session保存到本地文件)
    # 将cookie写入文件,并保存到self.co变量
    # 在登陆完成之后做此操作
    self.cookie_jar = response.meta['cookiejar']
    self.cookie_jar.extract_cookies(response, response.request)
    
    try:
    with open("cookie.txt", 'wb+') as f:
    for cookie in self.cookie_jar:
    f.write(str(cookie) + '
    ')
    
    self.co = str(cookie).split(' ')[1]
    self.co = {self.co.split('=')[0]: self.co.split('=')[1]}
    except Exception, e:
    print e
    
    ···
    
    # 如果写入过cookie,则读取出来
    if os.path.isfile('cookie.txt'):
    with open("cookie.txt") as f:
    cookiejar = f.read()
    self.co = cookiejar.split(' ')[1]
    self.co = {self.co.split('=')[0]: self.co.split('=')[1]}
    
    • 另一种表单提交方案:
    return scrapy.FormRequest.from_response(
    response,
    meta = {'cookiejar' : response.meta['cookiejar']},
    formnumber = 1,
    formdata = self.post_data,
    dont_click = True,
    callback = self.parse_page,
    )
    
  • 相关阅读:
    这些特效对于学习前端我们很有用
    每周前端开源推荐第二期
    每周前端开源推荐第七期
    BeeFramework:以极客的方式开发你的应用
    node-webkit文档翻译#package.json
    android批量文件上传(android批量图片上传)
    VC多线程临界区
    【美妙的Python之三】Python 对象解析
    (转载)屌丝从毕业时的月入3000到三年后亿万身家的精彩励志之旅
    fedora 安装 pidgin-lwqq
  • 原文地址:https://www.cnblogs.com/sanmu083/p/5532082.html
Copyright © 2011-2022 走看看