Scrapy Login Authentication

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Scrapy Login Authentication



I am fairly new to scraping, and I have a page I would like to login to so that I can scrape.



From the Scrapy documentation, it appears we should use FormRequest. Here is the example that is given in the documentation:


import scrapy

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):
# check login succeed before going on
if "authentication failed" in response.body:
self.logger.error("Login failed")
return

# continue scraping with authenticated session...



However, this seems oversimplified in most cases. I have looked at other examples online where the login process requires tokens, etc.



I think I have identified the form I need to post in order to login to the site I am trying to scrape.


<form method="post" action="/" id="MainForm">
<div id="lgn-container" class="box sign-in">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" id="hfLoginDsId" value="{1234567}" />



I'm wondering how I can edit the example code given in the documentation to reflect this particular scenario. I need to give a username and password, but I don't see how that fits in here!









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived