如何使用Scrapy解析xml文件中的html项嵌入项

时间:2012-12-08 22:08:15

标签: xml scrapy

我正在尝试使用XMLFeedSpider解析xml提要

在XML Feed中,我想提取“价格”项:

<span class="price" id="product-price-2037">19,77 €</span>

但是这个价格项目在标签内的html代码中如下:

<channel>
<item>
<title>
<![CDATA[ product title ]]>
</title>
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
<link>http://example.com/apage.html</link>
<description>
<![CDATA[
<table><tr><td><a href="http://example.com/apage.html">
<img src="http://example.com/media/catalog/product/aimage173.jpg" border="0" align="left" height="75" width="75"></a></td>
<td style="text-decoration:none;"> <div class="price-bframe"> <p class="old-price"> <span class="price-label">Prix normal :</span>
<span class="price" id="old-price-2895037">40,00 €</span> </p>
<p class="special-price"> <span class="price-label">Prix spécial :</span>
<span class="price" id="product-price-2037">19,77 €</span> </p> </div> </td></tr></table>
]]>
</description>
</item>
</channel>

这是我的实际蜘蛛:

from scrapy.contrib.spiders import XMLFeedSpider
from scrapy.selector import XmlXPathSelector
from tutorial.items import DmozItem

class DmozSpider(XMLFeedSpider):
name = 'myspidername'
allowed_domains = ["example.com"]
start_urls = ['http://example.com/rss/catalog/new/store_id/1/']
iterator = 'iternodes'
itertag = 'channel'

def parse_node(self, response, node):
    title = node.select('item/title/text()').extract()
    link = node.select('item/link/text()').extract()
    price = node.select('*[@class=price"]text()').extract()
    item = DmozItem()
    item['title'] = title
    item['link'] = link
    item['price'] = price
    return item

结果:

Invalid Xpath: *[@class=price"]text()

1 个答案:

答案 0 :(得分:1)

我认为这是因为你的路径无效试试这个

[@class=price"]/text()

我认为你错过了文本之前的斜杠