只想知道如何将请求以及登录凭证发送到登录页面以获取数据。
网站通常通过元素提供预填充的表单字段,例如会话相关数据或身份验证令牌(用于登录页面)。抓取时,您会希望自动填充这些字段,并且仅覆盖其中的几个字段,例如用户名和密码。您可以将FormRequest.from_response()方法用于此作业。这是一个使用它的蜘蛛示例:import scrapy
def authentication_failed(response):
# TODO: Check the contents of the response and return True if it failed
# or False if it succeeded.
pass
class LoginSpider(scrapy.Spider):
name = 'example.com'
start_urls = ['http://www.example.com/users/login.php']
def parse(self, response):
return scrapy.FormRequest.from_response(
response,
formdata={'username': 'john', 'password': 'secret'},
callback=self.after_login
)
def after_login(self, response):
if authentication_failed(response):
self.logger.error("Login failed")
return
# continue scraping with authenticated session...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句