[英]How do I pass form data with Scrapy from the command line?
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...
Open terminal and make sure scrapy installed. 打开终端并确保安装了scrapy。
scrapy shell
from scrapy.http import FormRequest
request=FormRequest(url='http://www.example.com/users/login.php',formdata={'username': 'john','password':'secret',})
Information: 信息:
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...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.