资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

pythonscrapy.Request怎么发送请求

这篇文章主要讲解了“python scrapy.Request怎么发送请求”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python scrapy.Request怎么发送请求”吧!

成都创新互联主营江门网站建设的网络公司,主营网站建设方案,app软件定制开发,江门h5微信小程序定制开发搭建,江门网站营销推广欢迎江门等地区企业咨询

说明

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

感谢各位的阅读,以上就是“python scrapy.Request怎么发送请求”的内容了,经过本文的学习后,相信大家对python scrapy.Request怎么发送请求这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


文章标题:pythonscrapy.Request怎么发送请求
浏览路径:http://cdkjz.cn/article/iepihj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220