【发布时间】:2018-09-23 12:42:58
【问题描述】:
我需要判断 Scrapy 蜘蛛下载的页面是否为 html。我希望蜘蛛抓取的网站有 pdf 和 html 链接的组合。因此,如果遇到 pdf 文件,它将通过 PDFReader 发送响应,否则它将按原样读取 html 文件。这是我的代码的一部分,但它不起作用:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "spyder_OLD"
allowed_domains = ['doc.scrapy.org']
start_urls = ['https://doc.scrapy.org/en/latest/index.html']
def parse(self, response):
ct = response.headers.get("content-type", "").lower()
return ct
我将蜘蛛的结果输出到一个 .csv 文件,但它始终是空的。只是有ct = response.headers 会输出整个头信息,这是没用的。我该怎么办?
编辑: 我终于设法返回了字典,但仍然无法提取相关信息:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "spyder_OLD"
allowed_domains = ['doc.scrapy.org']
start_urls = ['https://doc.scrapy.org/en/latest/index.html']
def parse(self, response):
ct = {"content-type": response.headers.get("content-type", "").lower()}
return ct["content-type"]
将上述内容输出到 .csv 文件仍会返回一个空白文件,尽管 output ct 返回一个包含两行的 .csv 文件:content-type 和 text/html。如何仅提取答案的“html”文本部分?
【问题讨论】:
-
不确定是不是
Content-Type:???? -
不,这行不通。