5

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
  • 10,536
  • 6
  • 41
  • 60
Theodis Butler
  • 136
  • 2
  • 9

2 Answers2

8

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
  • 3,039
  • 2
  • 15
  • 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
  • 20,518
  • 4
  • 53
  • 66