【发布时间】:2015-10-09 01:01:19
【问题描述】:
我一直在尝试将一些嵌套文本与 Scrapy 中的 xpath 连接在一起。我认为它使用 xpath 1.0?我看过很多其他帖子,但似乎没有什么能得到我想要的
这里是html的具体部分(实际页面http://adventuretime.wikia.com/wiki/List_of_episodes):
<tr>
<td colspan="5" style="border-bottom: #BCD9E3 3px solid">
Finn and Princess Bubblegum must protect the <a href="/wiki/Candy_Kingdom" title="Candy Kingdom">Candy Kingdom</a> from a horde of candy zombies they accidentally created.
</td>
</tr>
<tr>
<td colspan="5" style="border-bottom: #BCD9E3 3px solid">
Finn must travel to <a href="/wiki/Lumpy_Space" title="Lumpy Space">Lumpy Space</a> to find a cure that will save Jake, who was accidentally bitten by <a href="/wiki/Lumpy_Space_Princess" title="Lumpy Space Princess">Lumpy Space Princess</a> at Princess Bubblegum's annual 'Mallow Tea Ceremony.'
</td>
</tr>
(much more stuff here)
这是我想要返回的结果:
[u'Finn and Princess Bubblegum must protect the Candy Kingdom from a horde of candy zombies they accidentally
created.\n', u'Finn must travel to Lumpy Space to find a cure that will save Jake, who was accidentally bitten', (more stuff here)]
我尝试使用来自 HTML XPath: Extracting text mixed in with multiple tags?
description =sel.xpath("//table[@class='wikitable']/tr[position()>1]/td[@colspan='5']/parent::tr/td[descendant-or-self::text()]").extract()
但这只会让我回来
[u'<td colspan="5" style="border-bottom: #BCD9E3 3px solid">Finn and Princess Bubblegum must protect the <a href="/wiki/
Candy_Kingdom" title="Candy Kingdom">Candy Kingdom</a> from a horde of candy zombies they accidentally created.\n</td>',
string() 的答案似乎对我也不起作用...我得到一个只有一个条目的列表,而且应该还有更多。
我得到的最接近的是:
description = sel.xpath("//table[@class='wikitable']/tr[position()>1]/td[@colspan='5']//text()").extract()
这让我回来了
[u'Finn and Princess Bubblegum must protect the ', u'Candy Kingdom', u' from a horde of candy zombies they accidentally
created.\n', u'Finn must travel to ', u'Lumpy Space', u' to find a cure that will save Jake, who was accidentally bitten, (more stuff here)]
有人知道关于连接的 xpath 技巧吗?
谢谢!!
编辑:蜘蛛代码
class AT_Episode_Detail_Spider_2(Spider):
name = "ep_detail_2"
allowed_domains = ["adventuretime.wikia.com"]
start_urls = [
"http://adventuretime.wikia.com/wiki/List_of_episodes"
]
def parse(self, response):
sel = Selector(response)
description = sel.xpath("//table[@class='wikitable']/tr[position()>1]/td[@colspan='5']//text()").extract()
print description
【问题讨论】:
标签: python html xpath web-scraping scrapy