Scrapy 带有基本HTML页面的刮擦选择器没有输出

Scrapy 带有基本HTML页面的刮擦选择器没有输出,scrapy,Scrapy,我很难让基本的(非常基本的)html页面用我正在使用的爬行器输出任何东西,希望有人能让我走上正确的道路 我正在尝试刮取的html示例: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <head> <link rel="shortcut icon" href="../images/favicon.ico"> <meta http-equiv="Content-Type" c

我很难让基本的(非常基本的)html页面用我正在使用的爬行器输出任何东西,希望有人能让我走上正确的道路

我正在尝试刮取的html示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>
<link rel="shortcut icon" href="../images/favicon.ico">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="../include/default.css" type="text/css">
<meta name="Author" content="Author">
<title>Article Title</title>
</head>

<body>

<h3>Month Day, Year</h3>

<hr size="1">

<h4>Article Title Here:</h4>

<p>paragraph 1, Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo</p>

<p>paragraph 2. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.</p>

<p>paragraph 3, Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.</p>

<p>closing, Sed ut perspiciatis unde omnis iste natus </p>

<hr size="1">

</body>
</html>

我假设我过于简化了xpath规则?

如果您的爬行器是爬行器的子类,它不应该覆盖默认的解析回调(爬行器类在内部使用)。当您开始使用Scrapy时,这有点令人困惑,它可能会在将来的版本中得到解决

在您发布的代码中,您没有使用爬行蜘蛛规则,因此您可以问问自己是否真的需要从爬行蜘蛛继承。只能从
scrapy.Spider
继承

XPath表达式看起来不错,但是
.XPath()
方法只返回一个选择器,您缺少对
.extract()
方法的调用。此外,您可能不需要实例化选择器,如果您使用的是Scrapy 0.24+,您只需执行以下操作:

def parse(self, response):
    date = response.xpath('//h3').extract()
    title = response.xpath('//h4').extract()
    body = response.xpath('//p').extract()
    yield item

您可能需要阅读我编写的Scrapy教程,该教程试图让您快速入门:

您几乎走上了正确的道路,您需要使用extract()方法,该方法将返回元素列表,如果您刚开始学习,也许我创建的关于Scrapy的幻灯片可以帮助您:D

def parse(self, response):
    date = response.xpath('//h3').extract()
    title = response.xpath('//h4').extract()
    body = response.xpath('//p').extract()
    yield item