python - Python / Scrapy:如何确定页面是否为html?
问题描述
我需要确定 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”文本部分?
解决方案
不确定它是否还在。但听起来 builtwith 模块可能对你有用?
它向您展示了正在实现的各种 javascript 框架、Web 框架和 Web 服务器。您可以搜索网络框架并确定它们是否用于动态加载内容。
您可以: pip install builtwith
推荐阅读
- facebook - Facebook Insights API - 使用子代码 99 获取错误代码 1
- amazon-web-services - 如何将带有参数的命名参数传递给aws batch?
- php - 当我尝试在 MYSQL 上插入数据时出现 php 问题
- pyomo - 如何从 Pyomo 检索约束值
- popper.js - 使用 popper.js 隐藏和取消隐藏文本
- terraform - 使用地图查找将 Terraform 插值到 var
- javascript - Jquery MultiSelect Dropdown 无法正确处理来自 Ajax 调用的数据
- ruby - 在 Windows 10 上安装 Ruby 2.3.2
- c# - 连接到 Azure 文件存储上的 SQLite 数据库
- javascript - 无法匹配任何路由。角度误差?