简体   繁体   English

如何从命令行使用Scrapy传递表单数据?

[英]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。

  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 Scrapy 1.0.0

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.

相关问题 如何从命令行传递文件名并在genfromtxt中使用它? - How do I pass a filename from the command line and use it in genfromtxt? 从命令行将参数传递给scrapy规则或动态修改规则 - pass arguments to scrapy rules from command line or dynamically modify rules 如何访问通过 scrapy 中的 -s 传递的命令行参数? - How to access a command line argument that pass by -s in scrapy? 如何在 Scrapy 中没有表单的情况下从输入中获取数据 - How can I get data from input without a form in Scrapy 如何为从批处理文件运行的python脚本传递变量命令行参数? - How do I pass variable command line arguments for a python script, that runs from a batch file? 如何在调试模式下将命令行参数从 VS 传递给 Python? - How do I pass command line arguments to Python from VS in Debug mode? 此抓取代码将数据输入错误的格式(搜索格式)。 我如何获得实际的登录表格 - This scrapy code enters data into the wrong form (search form). How do i get to the actual login form 如何在 pytest 中传递命令行参数? - How do I pass command line args in pytest? 我是否需要传递所有隐藏的字段以及scrapy中的表单 - Do i need to pass all the hidden fields as well with the form in scrapy 如何使用 xpath (Scrapy) 从 dict 获取数据 - How do I get data from dict using xpath (Scrapy)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM