Scrapy 登入知乎

2021-09-19 10:00:28 字數 1893 閱讀 5819

需要先建立乙個scrapy專案,然後建立乙個testspider

按照需要修改items

然後開始修改testspider的內容,**中都有注釋,不作贅述

# -*- coding: utf-8 -*-

import scrapy

import os

import time

# mode 1:tencent 2:free

mode = 2

proxy = "" if mode == 1 else ""

#設定 使用者名稱和密碼

email = "***[email protected]"

password = "password"

class testspider(scrapy.spider):

name = 'testspider'

zhihu_url = ""

login_url = "/login/email"

domain = ""

# 設定 headers

headers_dict =

def start_requests(self):

yield scrapy.request(

url=self.zhihu_url,

headers=self.headers_dict,

meta=,

callback=self.request_captcha

)def request_captcha(self, response):

# 獲取_xsrf值

_xsrf = response.css('input[name="_xsrf"]::attr(value)').extract()[0]

# 獲得驗證碼的位址

captcha_url = "" + str(time.time() * 1000)

# 獲取請求

yield scrapy.request(

url=captcha_url,

headers=self.headers_dict,

meta=,

callback=self.download_captcha

)def download_captcha(self, response):

with open("captcha.gif", "wb") as fp:

fp.write(response.body)

# 開啟驗證碼

os.system('open captcha.gif')

# 輸入驗證碼

print "請輸入驗證碼:\n"

captcha = raw_input()

# 輸入賬號和密碼

yield scrapy.formrequest(

url= self.login_url,

headers= self.headers_dict,

formdata=,

meta=,

callback=self.request_zhihu

)def request_zhihu(self, response):

'''現在已經登入,請求www.zhihu.com的頁面

'''yield scrapy.request(url = self.zhihu_url,

headers = self.headers_dict,

meta = },

},callback = self.end_login,

dont_filter = true)

def end_login(self, response):

#至此已經登入

with open("base.html", "wb") as fp:

fp.write(response.body)

Scrapy模擬登入知乎

scrapy startproject zhihu login scrapy genspider zhihu www.zhihu.com coding utf 8 import scrapy class zhihuspider scrapy.spider name zhihu allowed dom...

利用scrapy模擬登入知乎

閒來無事,寫乙個模擬登入知乎的小demo。分析網頁發現 登入需要的手機號,密碼,xsrf引數,驗證碼 實現思路 1 獲取驗證碼 2 獲取 xsrf 引數 3 攜帶引數,請求登入 驗證碼url format t t t 為時間戳 登入介面url 手機登入申請url 實現 首先配置檔案 settings...

python 爬蟲 登入知乎

pytho3.4 requests.get request.post requests.session 1 通過瀏覽器f12開發者工具,找到登入時提交的表單以及請求的url 注意請求的url不是瀏覽器網域名稱框的url 因為知乎改了,現在不需要驗證碼了,所以中少了驗證碼的引數,不過 裡是有的 2 設...