我正在为我的 IT 工作开展一个项目,该项目要求我使用 Scrapy/XPath 编写一个爬虫,以从一个相当简单的 HTML 页面获取一组相当简单的数据。我已经按照我想要的方式工作,除了一些斜体文本(被抓取的网站是一个语言教育项目,在这个特定的文本字段中有很多斜体实例)没有显示。
下面是我在斜体问题出现之前成功使用的代码:
rawTitles = []
for sel in response.xpath('//h2[@class="video"]'):
rawTitle = sel.xpath('text()').extract()
rawTitles.append(rawTitle[0])
print rawTitles
“print rawTitles”得到以下返回:
[u'\n', u'\nVariations in Making ', u'\nMaking ', u'\nCommon Rice and Meat Dishes', u'\nRumens and ']
我想要的是这样的:
[u'\n<i>Mjadra</i>', u'\nVariations in Making <i>Mansaf</i>', u'\nMaking <i>Maqloobeh</i>', u'\nCommon Rice and Meat Dishes', u'\nRumens and <i>Mahashi</i>']
如果文字 HTML 标签不能包含在输出中,我至少希望包含纯文本。单词应该出现的空白似乎不是我能做的最好的。
有人知道我可能想尝试什么吗?如果我没有提供足够的信息,请告诉我。提前致谢。
编辑:这是我需要从中提取信息的表条目示例:
<td width="25%" valign="top" align="center">
<h2 class="video"><img src="content/pl_makingfood_mjadrah.jpg" alt="Thumbnail image from video" width="160" height="120" /><br /><br />
<i>Mjadra</i></h2> <p class="video">Video <br />
<a href="content/pl_makingfood_mjadrah.rm" class="main">real</a>
<a href="content/pl_makingfood_mjadrah.mp4" class="main" target="_blank">mp4</a><br /><br />
Palestinian Arabic & English <br />
<a href="content/pl_makingfood_mjadrah.doc" target="_blank" class="main"> doc </a>
<a href="content/pl_makingfood_mjadrah.pdf" target="_blank" class="main"> pdf </a></p>
</td>
请您参考如下方法:
让我们看看 scrapy shell 中的不同提取模式,从您的示例 HTML 构建一个选择器:
>>> import scrapy
>>> t = '''<td width="25%" valign="top" align="center">
... <h2 class="video"><img src="content/pl_makingfood_mjadrah.jpg" alt="Thumbnail image from video" width="160" height="120" /><br /><br />
... <i>Mjadra</i></h2> <p class="video">Video <br />
...
... <a href="content/pl_makingfood_mjadrah.rm" class="main">real</a>
... <a href="content/pl_makingfood_mjadrah.mp4" class="main" target="_blank">mp4</a><br /><br />
...
... Palestinian Arabic & English <br />
... <a href="content/pl_makingfood_mjadrah.doc" target="_blank" class="main"> doc </a>
... <a href="content/pl_makingfood_mjadrah.pdf" target="_blank" class="main"> pdf </a></p>
... </td>'''
>>> selector = scrapy.Selector(text=t, type="html")
首先,让我们循环 <h2 class="video">元素(使用 CSS 选择器),并提取循环内每个标题的字符串表示形式:
>>> for h2 in selector.css('h2.video'):
... print(h2.xpath('string()').extract())
...
['\nMjadra']
我们松开了 <i>信息。
让我们尝试只获取文本节点(使用 text() 节点测试):
>>> for h2 in selector.css('h2.video'):
... print(h2.xpath('text()').extract())
...
['\n']
比以前更糟糕的是,我们没有在 <i> 中获取文本节点元素。 (实际上,text() 只选择直接子文本节点,而不是子节点的子节点)
让我们试试 .// ,又名 ./descendant-or-self::node()/捷径:
>>> for h2 in selector.css('h2.video'):
... print(h2.xpath('.//text()').extract())
...
['\n', 'Mjadra']
并不比使用 XPath 的 string() 好多少.
现在,让我们使用 node()节点测试,捕获元素和文本节点:
>>> for h2 in selector.css('h2.video'):
... print(h2.xpath('node()').extract())
...
['<img src="content/pl_makingfood_mjadrah.jpg" alt="Thumbnail image from video" width="160" height="120">', '<br>', '<br>', '\n', '<i>Mjadra</i>']
更好,但我们有这些 <img>您可能不想要的标签。所以让我们只选择文本节点和<i>小号:
>>> for h2 in selector.css('h2.video'):
... print(h2.xpath('./node()[self::text() or self::i]').extract())
...
['\n', '<i>Mjadra</i>']
>>>
您可能希望从每个标题中获取一个字符串。所以使用 Python 的 join()是一个选项:
>>> for h2 in selector.css('h2.video'):
... print( "".join(h2.xpath('./node()[self::text() or self::i]').extract()) )
...
<i>Mjadra</i>
>>>
