import scrapy
class LoginSpider(scrapy.Spider):
name = 'login'
start_urls = ['https://github.com/login']
def parse(self, response):
ever_token = response.xpath('//*[@id="login"]/div[4]/form/input[1]/@value').extract_first()
timestamp_secret = response.xpath('//*[@id="login"]/div[4]/form/div/input[11]/@value').extract_first()
timestamp = response.xpath('//*[@id="login"]/div[4]/form/div/input[10]/@value').extract_first()
data = {
"commit": "Sign in",
"authenticity_token": ever_token, # token每次都会变,可以在login网页中查找到
"login": "xxx", # 填成自己的
"password": "xxx", # 填成自己的
"webauthn-support": "supported",
"webauthn-iuvpaa-support": "unsupported",
"return_to": "https://github.com/login",
"timestamp": timestamp,
"timestamp_secret": timestamp_secret
}
yield scrapy.FormRequest( # 用的就是post方式
url='https://github.com/session', # post网址
callback=self.after_login,
formdata=data
)
def after_login(self, response):
yield scrapy.Request('https://github.com/yezhoubing', callback=self.check_login)
def check_login(self, response):
print(response.xpath('html/head/title/text()').extract_first())
用scrapy.FormRequest()方法发送post请求