登录

scrapy post请求登录

# -*- coding: utf-8 -*-
import scrapy


class ChoutiSpider(scrapy.Spider):
    name = 'chouti'
    allowed_domains = ['chouti.com']
    start_urls = ['http://chouti.com/']
    # 第一次请求之后返回的响应
    # 有的网站在返回登录页面时,会携带一些登录需要的参数,例如csrf_token,xsrf等等
    # 需要先从登录页面中提取所需的参数,再发送post请求

    def parse(self, response):
        '''
        通过发送post请求模拟登录
        :param response:
        :return:
        '''
        # 重新发起一次登陆请求
        # FormRequest() 是scrapy提供的用于发送post请求的类
        yield scrapy.FormRequest(
            url='http://chouti.com/login',
            formdata={
                'phone': '8612344555555',
                'password': '123456',
                'oneMonth': '1'
            },
            callback=self.parse_index
        )

    def parse_index(self, response):
        # 登录成功之后再获取数据
        print(response.text)
        yield scrapy.Request(
            url='http://chouti.com/',
            callback=self.parse_index
        )


scrapy get请求登录

# urlencode 对参数进行编码

from scrapy import urlencode

有图片验证码 使用云打码获取验证码

from Tools import YDMHttp

def parse(self,response):

url = 'http://www.yundama.com/index/captcha'

response = requests.get(url)

with open('captcha.png','w+') as f:

f.write(response.content)

# 创建http对象

ydm = YDMHttp()

# 登录

ydm.login()

# 上传文件接受返回数据

cid,result = ydm.decode('captcha.png',5000,30)

# 准备用户名 密码 utype 验证码


data = {

'username' : 'jiaqianzhen',

'password' : '123456',

'utype':'1',

'vcode':result

}

parmars = urlencode(data)

# 拼接网址

login_url = 'http://www.yundama.com/' + parmars

scrapy.Request(

url=login_url,

callback=self.parse_index)

def parse_link(self, response)

yield

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章