【问题标题】:Scrapy 1.8.0 returns error 500, but Python code returns success 200Scrapy 1.8.0 返回错误 500,但 Python 代码返回成功 200
【发布时间】:2020-01-28 12:09:02
【问题描述】:

地址是:'https://planningapi.agileapplications.co.uk/api/application/search?reference=GDO+19%2F12'

我可以通过 Python requests 库轻松下载此页面:

headers = {
            'x-client': 'EXMOOR',
            'x-product': 'CITIZENPORTAL',
            'x-service': 'PA',
            }
url='https://planningapi.agileapplications.co.uk/api/application/search?reference=GDO+19%2F12'
resp = requests.get(url, headers=headers)

或者我可以通过 CURL 轻松下载页面:

curl 'https://planningapi.agileapplications.co.uk/api/application/search?reference=GDO+19%2F12' -H 'x-product: CITIZENPORTAL' -H 'x-service: PA' -H 'x-client: EXMOOR'

它们都返回状态 200 结果:

{"total":1,"results":[{"id":18468,"reference":"GDO 19/12","proposal":"Prior notification for excavations to bury tanks and trenches to lay water pipes","location":"Land North West of North and South Ley, Exford, Minehead, Somerset.","username":"","applicantSurname":"Mr & Mrs M Burnett","agentName":"JCH Planning Limited","decisionText":null,"registrationDate":"2019-10-04","decisionDate":"2019-10-30","finalGrantDate":null,"appealLodgedDate":null,"appealDecisionDate":null,"areaId":[],"wardId":[],"parishId":[3],"responded":null,"lastLetterDate":null,"targetResponseDate":null}]}

但是 Scrapy 返回状态 500 错误:

formdata = {'reference': 'GDO 19/12', }

headers = {
    'x-client': 'EXMOOR',
    'x-product': 'CITIZENPORTAL',
    'x-service': 'PA',
    }

fr = scrapy.FormRequest(
    url='https://planningapi.agileapplications.co.uk/api/application/search',
    method='GET',
    meta=response.meta,
    headers=headers,
    formdata=formdata,
    dont_filter=True,
    callback=self.ref_result_2,
    )

yield fr

可能是因为 Scrapy 将标头键大写(我尝试将它们取消大写,但 Twisted 做了同样的事情 - 它再次将它们大写),也许是出于其他原因。

如何调整我的 Scrapy 1.8.0 代码以成功获得与 Python 请求相同的结果?

【问题讨论】:

    标签: python python-3.x scrapy header twisted


    【解决方案1】:

    这实际上是由于 Scrapy 将标头字段大写造成的。如果您尝试在 cURL 命令中大写 then,您将得到与使用 Scrapy 相同的错误(您可以在爬虫类中的 Scrapy 设置 handle_httpstatus_list 中测试它,并在 parse 方法中打印 response.text )。正如您已经说过的那样,Twisted 也是如此,因此覆盖 scrapy.http.Headers 不是解决方案。

    但是,您可以根据this issue comment 做一个技巧来使 Twisted 不大写特定的标头:

    # -*- coding: utf-8 -*-
    from pprint import pprint
    import scrapy
    from twisted.web.http_headers import Headers as TwistedHeaders
    
    TwistedHeaders._caseMappings.update({
        b'x-client': b'x-client',
        b'x-product': b'x-product',
        b'x-service': b'x-service',
    })
    
    class Foo(scrapy.Spider):
        name = 'foo'
        handle_httpstatus_list = [500]
    
        def start_requests(self):
            formdata = {'reference': 'GDO 19/12'}
            headers = {
                'x-client': 'EXMOOR',
                'x-product': 'CITIZENPORTAL',
                'x-service': 'PA'
            }
            yield scrapy.FormRequest(
                'https://planningapi.agileapplications.co.uk/api/application/search',
                method='GET', headers=headers, formdata=formdata, callback=self.parse)
    
        def parse(self, response):
            pprint(response.text)
    

    现在您将获得结果。另一方面,根据RFC 7230,第 3.2 节,标题字段应该不区分大小写。

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      相关资源
      最近更新 更多