【问题标题】:Scrapy : (400 Bad Request)HTTP status code is not handled or not allowedScrapy:(400 Bad Request)HTTP状态码未处理或不允许
【发布时间】:2021-08-06 11:16:36
【问题描述】:

因为我是 python 和 scrapy 的新手。我一直在尝试抓取一个 URL 碎片化的网站。我正在发出发布请求以获取响应,但不幸的是它没有让我得到结果。

    def start_requests(self):
    try:
        form = {'menu': '6'
            , 'browseby': '8'
            , 'sortby': '2'
            , 'media': '3'
            , 'ce_id': '1428'
            , 'ot_id': '19999'
            , 'marker': '354'
            , 'getpage': '1'}

        head = {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            # 'Content-Length': '78',
            # 'Host': 'onlinelibrary.ectrims-congress.eu',
            # 'Accept-Encoding': 'gzip, deflate, br',
            # 'Connection': 'keep-alive',
            'XMLHttpRequest':'XMLHttpRequest',
        }

        urls = [
            'https://onlinelibrary.ectrims-congress.eu/ectrims/listing/conferences'
            ]

        request_body = urllib.parse.urlencode(form)
        print(request_body)
        print(type(request_body))

        for url in urls:
            req = Request(url=url, body= request_body, method='POST', headers=head,callback=self.parse)
            req.headers['Cookie'] = 'js_enabled=true; is_cookie_active=true;'

            yield req

    except Exception as e:
        print('the error is {}'.format(e))

我经常遇到错误

[scrapy.downloadermiddlewares.retry] ERROR: Gave up retrying <POST https://onlinelibrary.ectrims-congress.eu/ectrims/listing/conferences> (failed 4 times): 400 Bad Request

当我试图让邮递员检查相同的内容时,我得到了预期的输出。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: scrapy bad-request url-fragment


    【解决方案1】:

    如果要使用Request 发送POST 请求,则必须使用json.dumps() 将dictionary 转换为string。

    这是一个可行的解决方案:

    import scrapy
        
    class EventsSpider(scrapy.Spider):
        name = 'events'
    
        def start_requests(self):
            form = {'menu': '6', 'browseby': '8', 'sortby': '2', 'media': '3', 'ce_id': '1428', 'ot_id': '19999', 'marker': '354', 'getpage': '1'}
    
            head = {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'XMLHttpRequest': 'XMLHttpRequest',
            }
    
            url = 'https://onlinelibrary.ectrims-congress.eu/ectrims/listing/conferences'
            request_body = json.dumps(form)
            req = scrapy.Request(url=url, body=request_body, method='POST', headers=head, callback=self.parse)
            yield req
    
        def parse(self, response):
            print(response.json().keys())
    

    输出:

    dict_keys(['html', 'type', 'debug', 'total_pages', 'current_page', 'total_items', 'login'])
    

    额外提示:如果您可以在 Postman 中使用它,您可以单击右侧面板上的代码按钮,该按钮看起来像 &lt;/&gt;。如果选择 Python,您将获得使用 requests 库生成的代码。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我也尝试过,但仍然没有出现同样的错误
      • 也看看例如 req = FormRequest.from_response( 响应,然后是你的表单数据
      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多