scrapy.Request发送请求的方式

670次阅读
没有评论

scrapy.Request发送请求的方式

Request发送请求说明

1、使用scrapy.Request()指定method,body参数发送post请求。

2、使用scrapy.FormRequest()发送post请求,也可以发送表格和ajax请求。

Request发送请求实例

import scrapy
 
 
class Git2Spider(scrapy.Spider):
    name = 'git2'
    allowed_domains = ['github.com']
    start_urls = ['http://github.com/login']
 
    def parse(self, response):
        username = 'GitLqr'
        password = 'balabala'
 
        # 从登录页面响应中解析出post数据
        token = response.xpath('//input[@name="authenticity_token"]/@value').extract_first()
 
        post_data = {
            'commit': 'Sign in',
            'authenticity_token': token,
            'login': username,
            'password': password,
            'webauthn-support': 'supported',
        }
        print(post_data)
 
        # 针对登录url发送post请求
        yield scrapy.FormRequest(
            url='https://github.com/session',
            callback=self.after_login,
            formdata=post_data
        )
 
    def after_login(self, response):
        yield scrapy.Request('https://github.com/GitLqr', callback=self.check_login)
 
    def check_login(self, response):
        print(response.xpath('/html/head/title/text()').extract_first())
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-03-15发表,共计1114字。
新手QQ群:570568346,欢迎进群讨论 Python51学习