知行编程网知行编程网  2022-11-10 13:00 知行编程网 隐藏边栏  6 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python scrapy.Request发送请求的方式的相关知识,希望可以帮到处于编程学习途中的小伙伴

python scrapy.Request 发送请求的方式


说明

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

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


实例

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())


本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享