scrapy startproject zhihu_login
scrapy genspider zhihu www.zhihu.com
# -*- coding: utf-8 -*-
import scrapy
class zhihuspider(scrapy.spider):
name = "zhihu"
allowed_domains = ["www.zhihu.com"]
def start_requests(self):
# 返回值必須是乙個序列
return [scrapy.request('')]
def parse(self, response):
print response
測試能不能正確返回, 返回結果是
[scrapy] debug: retrying (failed 1 times): 500 internal server error
在settings中假如user_agent再進行測試, 返回200, 說明是知乎驗證瀏覽器的問題, 到此可以成功請求到
debug: crawled (200) (referer: none)
_xsrf (在html中可以找到)
email
password
remember_me
# -*- coding: utf-8 -*-
import scrapy
class zhihuspider(scrapy.spider):
name = "zhihu"
allowed_domains = ["www.zhihu.com"]
def start_requests(self):
# 返回值必須是乙個序列
return [scrapy.request('', callback=self.login)]
def login(self, response):
print '-------' # 便於測試
_xsrf = response.xpath(".//*[@id='sign-form-1']/input[2]/@value").extract()[0]
print _xsrf
使用formrequest登入
def login(self, response):
print '-------' # 便於測試
_xsrf = response.xpath(".//*[@id='sign-form-1']/input[2]/@value").extract()[0]
print _xsrf
return [scrapy.formrequest(
url = '', # 這是post的真實位址
formdata=,
headers=self.headers,
callback=self.check_login,
)]
def check_login(self, response):
if json.loads(response.body)['r'] == 0:
yield scrapy.request(
'',
headers=self.headers,
callback=self.page_content,
dont_filter=true, # 因為是第二次請求, 設定為true, 預設是false, 否則報錯
)
# -*- coding: utf-8 -*-
import scrapy
import json
class zhihuspider(scrapy.spider):
name = "zhihu"
allowed_domains = ["www.zhihu.com"]
headers =
def start_requests(self):
# 返回值必須是乙個序列
return [scrapy.request('', callback=self.login)]
def login(self, response):
print '-------' # 便於測試
_xsrf = response.xpath(".//*[@id='sign-form-1']/input[2]/@value").extract()[0]
print _xsrf
return [scrapy.formrequest(
url = '', # 這是post的真實位址
formdata=,
headers=self.headers,
callback=self.check_login,
)]def check_login(self, response):
if json.loads(response.body)['r'] == 0:
yield scrapy.request(
'',
headers=self.headers,
callback=self.page_content,
dont_filter=true,
)def page_content(self, response):
with open('first_page.html', 'wb') as f:
f.write(response.body)
print 'done'
注:也是剛學scrapy, 暫時不知道怎麼處理驗證碼的情況, 還望大牛指教 利用scrapy模擬登入知乎
閒來無事,寫乙個模擬登入知乎的小demo。分析網頁發現 登入需要的手機號,密碼,xsrf引數,驗證碼 實現思路 1 獲取驗證碼 2 獲取 xsrf 引數 3 攜帶引數,請求登入 驗證碼url format t t t 為時間戳 登入介面url 手機登入申請url 實現 首先配置檔案 settings...
Scrapy 登入知乎
需要先建立乙個scrapy專案,然後建立乙個testspider 按照需要修改items 然後開始修改testspider的內容,中都有注釋,不作贅述 coding utf 8 import scrapy import os import time mode 1 tencent 2 free mod...
知乎改版使用restapi後模擬登入
首先是抓包分析,知乎的登入介面變為了 找下這個介面的引數,懶的乙個個複製了,瀏覽器裡抓來就貼這裡了,各位將就看下 signature 引數僅僅就是對一堆變數進行hmac sha1 hash得到的乙個值,對應的幾個引數,clientid,granttype,source都是不變的,唯一有變化的是tim...