表单请求碎片

2024-09-28 19:01:33 发布

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

我对斯皮奇和Python还不熟悉。我试图使用来自Scrapy示例的FormRequest,但似乎formdata参数没有解析来自“Air”的“[%”。有什么解决办法吗? 代码如下:

import scrapy
import re
import json
from scrapy.http import FormRequest

class AirfareSpider(scrapy.Spider):
    name = 'airfare'
    start_urls = [
    'http://www.viajanet.com.br/busca/voos-resultados#/POA/MEX/RT/01-03-2017/15-03-2017/-/-/-/1/0/0/-/-/-/-'
    ]

    def parse(self, response):
    return [FormRequest(url='http://www.viajanet.com.br/busca/resources/api/AvailabilityStatusAsync', 
       formdata={"Partner":{
                   "Token":"p0C6ezcSU8rS54+24+zypDumW+ZrLkekJQw76JKJVzWUSUeGHzltXDhUfEntPPLFLR3vJpP7u5CZZYauiwhshw==",
                   "Key":"OsHQtrHdMZPme4ynIP4lcsMEhv0=",
                   "Id":"52",
                   "ConsolidatorSystemAccountId":"80",
                   "TravelAgencySystemAccountId":"80",
                   "Name":"B2C"
                           },
                 "Air":[{
                   "Arrival":{
                   "Iata":"MEX",
                   "Date":"2017-03-15T15:00:00.000Z"
                        },
                 "Departure":{
                   "Iata":"POA",
                   "Date":"2017-03-01T15:00:00.000Z"
                  },
               "InBoundTime":"0",
               "OutBoundTime":"0",
               "CiaCodeList":"[]",
               "BookingClass":"-1",
               "IsRoundTrip":"true",
               "Stops":"-1",
               "FareType":"-"
               }],
              "Pax":{
                   "adt":"1",
                   "chd":"0",
                   "inf":"0"
              },
              "DisplayTotalAmount":"false",
              "GetDeepLink":"false",
              "GetPriceMatrixOnly":"false",
              "PageLength":"10",
              "PageNumber":"2"
              }
             , callback=self.parse_airfare)]

    def parse_airfare(self, response):
        data = json.loads(response.body)

Tags: importselfcomjsonfalsehttpparseresponse
1条回答
网友
1楼 · 发布于 2024-09-28 19:01:33

尝试使用FormRequest.from_response函数

https://doc.scrapy.org/en/latest/topics/request-response.html#using-formrequest-from-response-to-simulate-a-user-login

import scrapy

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):
        # check login succeed before going on
        if "authentication failed" in response.body:
            self.logger.error("Login failed")
            return

相关问题 更多 >