The start_url in scrapy is processed through start_requests, and the implementation code is as follows
# This is the source code
def start_requests(self):
cls = self.__class__
ifmethod_is_overridden(cls, Spider,'make_requests_from_url'):
warnings.warn("Spider.make_requests_from_url method is deprecated; it ""won't be called in future Scrapy releases. Please ""override Spider.start_requests method instead (see %s.%s)."%(
cls.__module__, cls.__name__
),) for url in self.start_urls:yield self.make_requests_from_url(url)else:for url in self.start_urls:yieldRequest(url, dont_filter=True)
So correspondingly, if the url in the start_url address is a url address that needs to be accessed after logging in, you need to rewrite the start_request method and manually add a cookie in it
Test account noobpythoner zhoudawei123
import scrapy
import re
classLogin1Spider(scrapy.Spider):
name ='login1'
allowed_domains =['github.com']
start_urls =['https://github.com/NoobPythoner'] #This is a page that can be accessed after logging in
def start_requests(self): #Refactor start_requests method
# This cookies_str is obtained by capture
cookies_str ='...' #Capture
# Cookies_str is converted to cookies_dict
cookies_dict ={i.split('=')[0]:i.split('=')[1]for i in cookies_str.split('; ')}yield scrapy.Request(
self.start_urls[0],
callback=self.parse,
cookies=cookies_dict
)
def parse(self, response): #Use regular expressions to match the username to verify whether the login is successful
# The regular match is the username of github
result_list = re.findall(r'noobpythoner|NoobPythoner', response.body.decode())print(result_list)
pass
We know that you can send post requests by specifying method and body parameters through scrapy.Request(); but usually scrapy.FormRequest() is used to send post requests
Note: scrapy.FormRequest() can send forms and ajax requests, please refer to https://www.jb51.net/article/146769.htm
Find the url address of the post: click the login button to capture the packet, and then locate the url address as https://github.com/session
Find the law of the request body: analyze the request body of the post request, and the parameters contained in it are in the previous response
Whether the login is successful: Observe whether the user name is included by requesting the personal homepage
import scrapy
import re
classLogin2Spider(scrapy.Spider):
name ='login2'
allowed_domains =['github.com']
start_urls =['https://github.com/login']
def parse(self, response):
authenticity_token = response.xpath("//input[@name='authenticity_token']/@value").extract_first()
utf8 = response.xpath("//input[@name='utf8']/@value").extract_first()
commit = response.xpath("//input[@name='commit']/@value").extract_first()
# Construct a POST request and pass it to the engine
yield scrapy.FormRequest("https://github.com/session",
formdata={"authenticity_token":authenticity_token,"utf8":utf8,"commit":commit,"login":"noobpythoner","password":"***"},
callback=self.parse_login
)
def parse_login(self,response):
ret = re.findall(r"noobpythoner|NoobPythoner",response.text)print(ret)
By setting COOKIES_DEBUG=TRUE in settings.py, you can see the cookie delivery process in the terminal
Recommended Posts