2015-07-21 31 views
0

我使用Scrapy蜘蛛,試圖從以下頁面中提取價格提取網站價格時:http://www.saxoprint.co.uk/shop/business-cardsScrapy代碼:文字與英國英鎊符號導致問題使用蜘蛛

尤其是我的元素感興趣的是: // * [@ id =「customerGrossValue」]

當我運行我的蜘蛛時,它發現它返回的元素u''。我懷疑這是用UTF編碼做的,並且是由「£」符號不是ASCII的事實引起的。

我運行Windows 7和Python 2.7

我已經做了相當多的這種搜索,並試圖編碼成UTF-8,但無濟於事。

我敢肯定這可能是簡單的事情,但它是非常令人沮喪的,所以任何幫助將感激地收到。

謝謝!

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
import scrapy 
from scrapy.selector import Selector 
from scrapy.http import Request 
from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 
from saxoprint.items import SaxoprintItem 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.contrib.loader import ItemLoader 
from scrapy.contrib.exporter import XmlItemExporter 


class saxoprintSpider(BaseSpider): 
    name = "saxoprint" 
    allowed_domains = ["saxoprint.co.uk"] 
    start_urls = [ 
    "http://www.saxoprint.co.uk/shop/business-cards" 
    ] 

    def parse(self, response): 
     selector_object = response.xpath('//div[starts-with(@class,"pricetableDIV")]') 
     for select in selector_object: 

      price_item = select.xpath('.//span[(@id="customerNetValue")]/text()').extract()[0].strip() 
      print price_item #test the value 

      item = SaxoprintItem(
       price=price_item 
      yield item 
+0

非常感謝!這對我有效。它幫助我解決了我沒有返回蜘蛛所需的文本。自那以後,我用Selenium來獲取我需要的內容。你的回答在幫助我走向正確的方向方面起了很大作用。謝謝 – Predica

回答

0

如果我理解正確,您想要將不僅包含數字的unicode字符串轉換爲數字。這應該工作:

>>> s = u'ę123' 
>>> int("".join([c for c in s if c.isdigit()])) 
123 

您可以通過字符字符串製作的只有那些數字(isdigit())字符的列表中進行迭代。之後,將列表轉換回字符串("".join(...)),最後纔將其轉換爲整數(int(...))。