2013-06-20 12 views
1

我正在用Scrapy報廢數據。目前我使用UTF-8對其進行編碼,即將detail_content.select('p/text()[1]').extract()[0].encode('utf-8')保存到JSON文件中,然後使用Django和移動應用程序再次顯示捕獲的文本。的\u00a3代替£編碼Scrapy數據以顯示在Django和Android中

在JSON文件中的轉義的HTML被使用Unicode 'blah blah \u00a34,000 blah'

現在我的問題是,當我嘗試,並顯示在Django模板實際的文字字符顯示文本或移動應用逃脫

我不應該在JSON中存儲轉義的Unicode嗎?使用JSON轉義將ASCII存儲在JSON文件中會更好嗎?如果是的話,你如何去做這個與scrapy?

鬥志旺盛代碼:

from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 
from scrapy.http import Request 
from scrapy.item import Item, Field 
import datetime 
import unicodedata 
import re 

class Spider(BaseSpider): 
    #spider stuff 

    def parse(self, response): 
     hxs = HtmlXPathSelector(response) 
     rows = hxs.select('//ul[@class = "category3"]/li') 
     for row in rows: 
      item = Item() 
      if len(row.select('div[2]/a/text()').extract()) > 0: 
       item['header'] = str(row.select('div[2]/a/text()') 
            .extract()[0].encode('utf-8')) 
      else: 
       item['header'] = '' 
      if len(row.select('div[2]/a/text()').extract()) > 0: 
       item['_id'] = str(row.select('div[2]/a/text()') 
            .extract()[0].encode('utf-8')) 
      else: 
       item['_id'] = '' 
      item['_id'] = self.slugify(item['_id'])[0:20] 
      item_url = row.select('div[2]/a/@href').extract() 
      today = datetime.datetime.now().isoformat() 
      item['dateAdded'] = str(today) 
      yield Request(item_url[0], meta={'item' : item}, 
          callback=self.parse_item) 

    def parse_item(self, response): 
     hxs = HtmlXPathSelector(response) 
     detail_content = hxs.select('//*[@id="content-area"]') 
     item = response.request.meta['item'] 
     item['description'] = str(detail_content.select('p/text()[1]') 
                 .extract()[0]) 
     item['itemUrl'] = str(detail_content.select('//a[@title="Blah"]/@href') 
                   .extract()[0]) 
     item['image_urls'] = detail_content.select('//img[@width="418"]/../@href') 
                     .extract() 
     print item 
     return item 
+0

您是否嘗試過不使用編碼('utf-8')'?其他問題,輸出是什麼:'detail_content.select('p/text()[1]')。extract()[0]'。我的意思是,這是'u'blah blah''還是''blah blah'' –

+0

另外,你如何輸出模板中的json? –

+0

是沒有編碼('utf-8'),我收到錯誤,如:'exceptions.UnicodeEncodeError:'ascii'編解碼器無法編碼字符u'\ u201c'在位置127:序號不在範圍內(128)' 我以常規方式輸出django模板中的文本,即'{{item.description}}'autoescaping on或off沒有任何區別 – KingFu

回答

0

確定這我覺得很奇怪:

item['header'] = str(row.select('div[2]/a/text()') 
        .extract()[0].encode('utf-8')) 

是不正確的做str(<some_value>.encode('utf-8'))。這基本上意味着你將一串utf-8字節轉換爲ascii。當utf-8字節超過128時,這可能會產生錯誤。

現在,我堅信你從Scrappy中獲得的角色已經在unicode中。

I receive errors like: exceptions.UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 127: ordinal not in range(128)

所以,我的建議是把代碼改成這樣:

item['header'] = row.select('div[2]/a/text()') 
       .extract()[0].encode('utf-8') 

只需卸下str()調用。這將得到從Scrappy收到的unicode,並將其轉化爲utf-8。一旦它在utf-8。字符串操作要小心。通常這種從unicode到特定編碼的轉換應該在寫入磁盤之前完成。

注意你有這樣的代碼在兩個地方。修改它們兩個。

更新:在這個看看,可能會有所幫助:scrapy text encoding

希望這有助於!

+0

你對str()沒有要求。不知道爲什麼我使用它,所以謝謝你!不過,我仍然遇到同樣的問題。原始HTML中沒有unicode轉義字符。Scrapy看起來像是在HTML發生變化之前將其轉換爲UTF-8 – KingFu

+0

對不起,我先不理解。所以在原始的HTML中沒有unicode scaped char。 Scrapy可能會做的是將它轉換爲Unicode,但不是'utf-8' –

+0

你仍然在你的.json文件中獲得\ u00a3? –