2014-01-13 63 views
3

我怎麼能從命令行傳遞用戶名和密碼?謝謝!如何從命令行通過Scrapy傳遞表單數據?

class LoginSpider(Spider): 
    name = 'example.com' 
    start_urls = ['http://www.example.com/users/login.php'] 

    def parse(self, response): 
     return [FormRequest.from_response(response, 
        formdata={'username': 'john', 'password': 'secret'}, 
        callback=self.after_login)] 

    def after_login(self, response): 
     # check login succeed before going on 
     if "authentication failed" in response.body: 
      self.log("Login failed", level=log.ERROR) 
      return 

     # continue scraping with authenticated session... 

回答

4

你可以做

scrapy crawl spidername -a username="john" -a password="secret" 

然後

class LoginSpider(Spider): 
    name = 'example.com' 
    start_urls = ['http://www.example.com/users/login.php'] 

    def parse(self, response): 
     return [FormRequest.from_response(response, 
        formdata={'username': self.username, 'password': self.password}, 
        callback=self.after_login)] 

    def after_login(self, response): 
     # check login succeed before going on 
     if "authentication failed" in response.body: 
      self.log("Login failed", level=log.ERROR) 
      return 

     # continue scraping with authenticated session... 
+0

嗨,我嘗試使用-a但scrapy外殼似乎不喜歡這個... – newdev14

4

打開終端,並確保安裝scrapy。

  1. scrapy shell

  2. from scrapy.http import FormRequest

  3. request=FormRequest(url='http://www.example.com/users/login.php',formdata={'username': 'john','password':'secret',})

信息:

  • 血肌酐apy 1.0.0
相關問題