在使用scrapy抓一个站时,网站使用了html和xml混编,在xml中把a标签用CDATA包起来了。
.....省略.....
<T>
<td>
<![CDATA[<a href="./xxxxx.htm" target="_blank" class='a2'>这里是标题</a>]]>
</td>
<td align='right'>2017-06-14</td>
</T>
.....省略.....
然后我用xpath拿到 <T> 节点后,使用了extract(),控制台打印出来变成这样了
.....省略.....
<T>
<td>
<a href="./xxxxx.htm" target="_blank" class='a2'>这里是标题</a>
</td>
<td align="right">2017-06-14</td>
</T>
.....省略.....
这样的话,我再用xpath拿 <T> 节点下面的 a 标签就拿不到了。这怎么破。
关键抓取代码如下
# 获取列表页主要信息
def parse(self, response):
response_constructor = HtmlResponse(response.url, body=response.body_as_unicode(), encoding='utf-8')
for article in Selector(response=response_constructor, type="xml").xpath('//xml[@id="DocumentsDataSrc"]/RECS/Documents/D/T'):
item = NewsItem()
#a标签这里就拿不到了
article_href = article.xpath('./a/@href').extract()