且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

强制 Python Scrapy 不编码 URL

更新时间:2022-11-12 20:50:04

在创建 Request 对象时,scrapy 应用了一些 url 编码方法.要恢复这些,您可以使用自定义中间件并根据需要更改 url.

When creating a Request object scrapy applies some url encoding methods. To revert these you can utilize a custom middleware and change the url to your needs.

您可以像这样使用 Downloader Middleware:

class MyCustomDownloaderMiddleware(object):

    def process_request(self, request, spider):
        request._url = request.url.replace("%5B", "[", 2)
        request._url = request.url.replace("%5D", "]", 2)

不要忘记像这样激活"settings.py 中的中间件:

Don't forget to "activate" the middleware in settings.py like so:

DOWNLOADER_MIDDLEWARES = {
    'so.middlewares.MyCustomDownloaderMiddleware': 900,
}

我的项目名为 so,文件夹中有一个文件 middlewares.py.您需要根据您的环境进行调整.

My project is named so and in the folder there is a file middlewares.py. You need to adjust those to your environment.