4

How could I pass username and password from the command line? Thanks!

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...
Stoic
  • 9,488
  • 5
  • 39
  • 60
Theodis Butler
  • 126
  • 1
  • 8

2 Answers2

7

Open terminal and make sure scrapy installed.

  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',})

Information:

  • Scrapy 1.0.0
Joni
  • 2,949
  • 2
  • 12
  • 10
6

you can do

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

and then

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...
paul trmbrth
  • 19,235
  • 3
  • 47
  • 62