python请求的数据属性在scrapy Reques中的位置

2024-04-20 14:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含数据的python请求,我必须用一个scrapy请求来替换它,但是我找不到方法,在scrapy请求中放置这个数据

data = '{"username":"xyz@gmail.com","password":"123"}'

response = requests.post('https://my.matterport.com/api/v2/user/login/', data=data)

我应该把数据放在哪里

Request('https://my.matterport.com/api/v2/user/login/', callback=self.foo)

或是用脏兮兮的形式


Tags: 数据方法httpscomapidatamyusername
3条回答
 request_with_cookies = Request(url="http://www.example.com",
                           cookies=[{'name': 'currency',
                                    'value': 'USD',
                                    'domain': 'example.com',
                                    'path': '/currency'}])

或者像这样尝试(不需要上课)

import scrapy

def authentication_failed(response):
    # TODO: Check the contents of the response and return True if it failed
    # or False if it succeeded.
    pass

class LoginSpider(scrapy.Spider):
    name = 'example.com'
    start_urls = ['http://www.example.com/users/login.php']

def parse(self, response):
    return scrapy.FormRequest.from_response(
        response,
        formdata={'username': 'john', 'password': 'secret'},
        callback=self.after_login
    )

def after_login(self, response):
    if authentication_failed(response):
        self.logger.error("Login failed")
        return

显示此链接可能会对您有所帮助,https://doc.scrapy.org/en/latest/topics/request-response.html

有两种方法可以执行此请求。一种方法是使用请求刮痧蜘蛛另一种方法是使用FormRequest。如果是简单的请求,您只需要提及请求的方法,该方法将在您的情况下发布,但是FormRequest默认设置为POST。你知道吗

如果使用刮擦请求:

form scrapy.spiders import Request
data = {"username":"xyz@gmail.com","password":"123"}
Request('https://my.matterport.com/api/v2/user/login/', method='POST', callback=self.foo, body=json.dumps(data))

如果使用FormRequest:

data = {"username":"xyz@gmail.com","password":"123"}
FormRequest('https://my.matterport.com/api/v2/user/login/',  formdata=data, callback=self.foo)

看起来你在找this part of the documentation。你知道吗

你试过了吗

FormRequest('https://my.matterport.com/api/v2/user/login/', 
            formdata=data, callback=self.foo)

什么?你知道吗

相关问题 更多 >