【发布时间】:2015-03-25 11:24:37
【问题描述】:
使用Scrapy 0.24 Selectors,我想提取包含其他元素内容的段落内容(在下面的示例中,它将是锚点<a>。我该如何实现呢?
代码
>>> from scrapy import Selector
>>> html = """
<html>
<head>
<title>Test</title>
</head>
<body>
<div>
<p>Hello, can I get this paragraph content without this <a href="http://google.com">Google link</a>?
</div>
</body>
</html>
"""
>>> sel = Selector(text=html, type="html")
>>> sel.xpath('//p/text()').extract()
[u'Hello, can I get this paragraph content with this ', u'?']
输出
[u'Hello, can I get this paragraph content with this ', u'?']
预期输出
[u'Hello, can I get this paragraph content with this Google link?']
【问题讨论】:
-
嗯。您可以先提取
<a hrefs... 中的内容,然后对其进行解析。如何开始的示例:stackoverflow.com/questions/3997525/python-replace-with-regex