• 技术文章 >Python技术 >Python基础教程

    python scrapy.Request发送请求的方式

    小妮浅浅小妮浅浅2021-08-12 09:21:35原创4128

    说明

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

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

    实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    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学习指路:python基础教程

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

    专题推荐:python scrapyrequest
    上一篇:python scrapy模拟登录的方法 下一篇:python字典的元素访问

    相关文章推荐

    • python字符串大小写转换的3种函数• python如何删除字符串的特殊字符• python两种数据类型的转换• python可变参数的两种传递方式• python import的本质探究• python Series如何进行相加• python如何查看hdf5文件• python scrapy如何建模• python scrapy处理翻页的方法• python如何重写start_requests方法• python scrapy模拟登录的方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网