- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个正在运行的 scrapy 项目,但它会占用大量带宽,因为它会尝试下载大量二进制文件(zip、tar、mp3 等)。
我认为最好的解决方案是根据 mimetype (Content-Type:) HTTP header 过滤请求。我查看了 scrapy 代码,发现了这个设置:
DOWNLOADER_HTTPCLIENTFACTORY = 'scrapy.core.downloader.webclient.ScrapyHTTPClientFactory'
我改成: DOWNLOADER_HTTPCLIENTFACTORY = 'myproject.webclients.ScrapyHTTPClientFactory'
并使用了一些 ScrapyHTTPPageGetter
,这里是突出显示的编辑:
class ScrapyHTTPPageGetter(HTTPClient):
# this is my edit
def handleEndHeaders(self):
if 'Content-Type' in self.headers.keys():
mimetype = str(self.headers['Content-Type'])
# Actually I need only the html, but just in
# case I've preserved all the text
if mimetype.find('text/') > -1:
# Good, this page is needed
self.factory.gotHeaders(self.headers)
else:
self.factory.noPage(Exception('Incorrect Content-Type'))
我觉得这是错误的,我需要更友好的方式来在确定它是不需要的 mimetype 后立即取消/删除请求。而不是等待下载全部数据。
编辑:
我特别询问这部分 self.factory.noPage(Exception('Incorrect Content-Type'))
是取消请求的正确方法。
更新 1:
我目前的设置已经让 Scrapy 服务器崩溃了,所以请不要尝试使用上面相同的代码来解决问题。
更新 2:
我已经使用以下结构设置了一个基于 Apache 的网站进行测试:
/var/www/scrapper-test/Zend -> /var/www/scrapper-test/Zend.zip (symlink)
/var/www/scrapper-test/Zend.zip
我注意到 Scrapy 会丢弃带有 .zip
扩展名的那些,但会丢弃没有 .zip 的那些,即使它只是指向它的符号链接(symbolic link)。
最佳答案
我构建这个中间件是为了排除任何不在正则表达式白名单中的响应类型:
from scrapy.http.response.html import HtmlResponse
from scrapy.exceptions import IgnoreRequest
from scrapy import log
import re
class FilterResponses(object):
"""Limit the HTTP response types that Scrapy dowloads."""
@staticmethod
def is_valid_response(type_whitelist, content_type_header):
for type_regex in type_whitelist:
if re.search(type_regex, content_type_header):
return True
return False
def process_response(self, request, response, spider):
"""
Only allow HTTP response types that that match the given list of
filtering regexs
"""
# each spider must define the variable response_type_whitelist as an
# iterable of regular expressions. ex. (r'text', )
type_whitelist = getattr(spider, "response_type_whitelist", None)
content_type_header = response.headers.get('content-type', None)
if not type_whitelist:
return response
elif not content_type_header:
log.msg("no content type header: {}".format(response.url), level=log.DEBUG, spider=spider)
raise IgnoreRequest()
elif self.is_valid_response(type_whitelist, content_type_header):
log.msg("valid response {}".format(response.url), level=log.DEBUG, spider=spider)
return response
else:
msg = "Ignoring request {}, content-type was not in whitelist".format(response.url)
log.msg(msg, level=log.DEBUG, spider=spider)
raise IgnoreRequest()
要使用它,请将它添加到 settings.py 中:
DOWNLOADER_MIDDLEWARES = {
'[project_name].middlewares.FilterResponses': 999,
}
关于Python Scrapy - 基于 mimetype 的过滤器以避免非文本文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13401382/
我正在使用 MediaRecorder API 在页面上录制一些媒体。在我的 MediaRecorder 初始化中,我没有指定内容类型,因为我不需要任何特别的内容。浏览器可以选择它想要的内容。 var
我有一个可以返回 text/html 或 application/pdf 内容的 servlet。显然,看起来 Internet Explorer (IE7) 没有正确处理应用程序/pdf。 例如。
我正在使用 C# 库 DotNetZip(Ionic.Zip 和 Ionic.Zlib)从目录生成电子书。目录如下所示: BookName | |___content/ | images/
我有一个带有二进制字段的文件模型 class File(models.Model): id = models.AutoField(primary_key=True) file = mo
覆盖模型保存方法时,有没有办法获取上传文件的内容类型?我试过这个: def save(self): print(self.file.content_type) super(Media,
我正在从 azure 读取文件。 mimetype 是 video/mp4 ,位于 response.headers["content-type"] 中。我找不到如何向文件添加 mimetypes,因
我想知道如果我运行这段代码会发生什么: MimeType Tester 我的内容将被解析为 XML 或 HTML?两个都?其他? 我还有一个 JS
我怎样才能从应用程序中获取 mimeType。 我正在开发一个基于 mimeType 过滤某些已安装应用程序的应用程序。 有人可以帮我解决这个问题吗? 非常感谢 最佳答案 mime 类型与 Activ
我需要获取项目对象的 MimeType: 我有什么:- 来自 EWS 的项目对象 (microsoft.exchange.webservices.data.Item) 我想要什么:- 该项目的 mim
我下载了一些文件,比如word、ppt、excel。但我不知道他们的 MIMETYPE 和后缀。有什么方法可以获取这些文件的 MIMETYPE? 最佳答案 如果您不知道后缀,则必须查看文件内容。通常,
如何使用 mimetypes 包中的 add_type() 函数添加新的 mimetype?我需要对 python 文件系统做些什么吗?如果我尝试类似这样的操作:add_type('text/new'
有人知道一种从字节数组中获取 mime 类型的方法吗?注意,我想在没有外部库的情况下做到这一点,只有 native java。 最佳答案 有一种方法可以使用 Java 7 来做到这一点,但它有点笨拙(
这只是一个用户上传一个文件。 最佳答案 UploadedFile.content_type 将返回上传时随文件一起发送的内容类型 header 。 如果您还需要在保存后检查文件,您可以使用 pytho
我正在尝试使用 Intent 打开一个 File,但 Android 没有打开适合该文件类型的应用程序。 使用以下代码,每个文件 - pdf、图像,所有内容 - 都使用音乐应用程序打开: Intent
从文件中使用 finfo 获取 mimetype $finfo = new finfo(FILEINFO_MIME); $type = $finfo->file($file); 从字符串中使用 fin
MIME::Types.type_for(filename) 我需要安装什么 gem,使用它需要什么文件? 最佳答案 gem install mime-types 和require 'mime/typ
我有两个节点应用程序在两个不同的端口上运行。 为了让 nginx 适本地路由请求,我有两个位置 block , # /etc/nginx/sites-available/my_site.com ser
如何在 xcode 中将 mimetype 转换为文件扩展名 例如我有这个代码 File Extension MIME Type aifc audio/x-aif
我正在考虑使用浏览器的 navigator.mimeTypes 数组作为第三级用户/浏览器标识符。例如,当我在 Chrome 上运行时... console.log(navigator.mimeTyp
我正在通过 MimeType 扫描目录。到目前为止,我的代码计算了目录中现有 MimiType 的总数: File dir = new File(dirPath); File[] files = di
我是一名优秀的程序员,十分优秀!