違いFormRequestとFormRequest.from_response

scrapy.FormRequest

  • 実現には、POSTリクエストを送信し、リクエストパラメータは、特殊なFormRequest機能を送信することで、サーバーにサイトの設計パラメータを満たしている必要があります。
1  クラスFormrequestSpider(CrawlSpider):
 2          名= ' githubの' 
3          allowed_domains = [ ' github.com ' ]
 4          start_urls = [ ' https://github.com/login ' ]
 5  
6          DEF (自己、応答)を解析:
 7              authenticity_token = response.xpath(" //入力【名@ = 'authenticity_token'] / @値" ).extract_first()
 8              UTF8 = response.xpath(" //入力/ @値[NAME = 'UTF8' @] " )。
extract_first()9              = response.xpath(コミット" //入力【名@ = 'コミット'] / @値" ).extract_first()
 10              post_data = dictの(
 11                  ログイン= " *********** "を12                  パスワード= " ********** " 13                  authenticity_token = authenticity_token、
 14                  UTF8 = UTF8、
 15は                  コミット= コミット、
 16  17              表单请求
18              収率scrapy.FormRequest(
 19                  " https://github.com/session " 20                  FORMDATA = post_data、
 21                  コールバック= self.after_login
 22  23  
24          DEF after_login(自己、応答):
 25              開く( "a.html" 、 "W"、エンコーディング= "UTF-8" Fとして):
26                   f.write(response.body.decode())
27              プリント(re.findall(" ******** "、応答。 body.decode()))

 

 

scrapy.FormRequest.from_response

  • 行動シミュレーションブラウザをクリックしFormRequest.from_responseすると、サーバのポストに要求を送信します
  • 唯一のウェブサイトをご覧形成んラベルの形で適用することができます
  • 唯一のWebデザインのラベルに記入する名前で入力された口座番号やパスワード、名前を気に。
1  クラスGithubSpider(CrawlSpider):
 2      名= ' github2 ' 
3。      allowed_domains = [ ' github.com ' ]
 4。      start_urls = [ ' https://github.com/login ' ]
 5。 
6。     DEF 構文解析(自己応答):
 7          降伏scrapy.FormRequest.from_response(
 8              応答、   から見てから自動応答フォーム
9人の             だけの単語典型的なログイン名とパスワードを渡す必要はいるFormDataは、辞書のキーはinputタグのname属性である
10              いるFormData = { ログイン" *********** "" パスワード"" ********** " }、
 11              コールバック= self.after_login
 12  13  
14      DEF after_login(自己、応答) :
 15          プリント(response.text)

 

参考読書:

https://www.cnblogs.com/ywjfx/p/11089248.html

おすすめ

転載: www.cnblogs.com/eddilelau/p/11619266.html