登錄

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

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