2014-01-31 22 views
1

由於描述中的特殊字符( ),我在下面的代碼中遇到了一個問題。由於這些特殊字符,我收到錯誤。如何刪除python中的特殊字符?

錯誤:'ascii'編解碼器無法解碼位置821中的字節0x94:序號不在範圍內(128) 請幫我刪除此錯誤。下面分別

鏈接

數據以文本形式和html表單數據給出:http://m.cellularoutfitter.com/p-85870-anycom-solar-bluetooth-car-kit_c.html(在頁面的末端具有描寫的特徵)

我已經嘗試了各種方法和編碼,但失敗了。

首先我得到鏈接的完整src然後通過使用x路徑我在變量中獲得描述。 由於某些原因,我無法發佈完整的代碼。遺憾

Python代碼:

parser = etree.HTMLParser(remove_blank_text=True, encoding="utf-8") 
tree = etree.HTML(popup_html, parser) 
    description = tree.xpath("//span[@itemprop='description' and not(src)] ") 

log.debug(str(description[0])) 
for desc in description: 
    log.debug(etree.tostring(desc,encoding='UTF-8')) 
    if etree.tostring(desc,encoding='UTF-8').find("IFRAME") < 0: 
     reply_dict['product_desc'] = reply_dict['product_desc'] + etree.tostring(desc,encoding='UTF-8') 
     reply_dict['product_desc'] = reply_dict['product_desc'].replace("&#13;\n", "").replace("\n", "<br/>").replace("img","").replace('< src="/productPics/altImgs/decal-skin-pdp-2.jpg"/>',"") 
     reply_dict['product_desc'] = reply_dict['product_desc'].replace("\xef\xbf\xbd","'") 
     reply_dict['product_desc'] = reply_dict['product_desc'].replace("\x92","'") 
     reply_dict['product_desc'] = "<br />".join(reply_dict['product_desc'].split("\n")).replace("  ", "&nbsp;").encode('ascii', 'xmlcharrefreplace') 

HTML代碼:

<div class="centerContain"> 
      Convenient Bluetooth car kit easily mounts to vehicle windshield and features high-performance solar panel capable of converting UV rays into Bluetooth battery power. What's included: ANYCOM Solar Bluetooth Car Kit, window mount, suction cups, 12/24V vehicle power adapter w/USB cable, 3M adhesive tape, user guide. 
      <ul> 
       <li>Solar panel recharges battery, providing 30 minutes of talk time for every 3 hours of sun light</li><li>Features Digital Signal Processing (DSP) technology, including compression and echo cancellation</li><li>Easily pairs with compatible devices</li><li>Bluetooth: v2.0</li><li>Talk Time: 15 hours</li><li>Standby Time: 25 days</li><li>Operating Range: 33 ft. (10 meters)</li><li>Size: 3.59� (H) x 1.98� (W) x 0.52� (D)</li><li>Weight: 2.11 oz.</li><li>Warranty: ANYCOM limited worldwide 2-year warranty</li> 
      </ul> 
     </div> 

以文本形式:

方便藍牙車載裝置容易安裝到車輛的擋風玻璃和功能能夠高性能太陽能電池板將紫外線轉換爲藍牙電池供電。包含的內容:ANYCOM太陽能藍牙車載套件,窗戶安裝,吸盤,帶USB線纜的12/24V車載電源適配器,3M膠帶,用戶指南。 太陽能電池板再充電電池中,每3小時太陽光的 功能數字信號處理(DSP)技術,包括壓縮和迴音消除 與兼容設備輕鬆對提供的通話時間30分鐘 藍牙:2.0 通話時間: 15小時 待機時間:25天 經營範圍:33英尺(10米) 尺寸:3.59 (高)x1.98 (寬)x0.52 (D) 重量:2.11盎司。 保修:ANYCOM全球有限保修2年

+0

在哪裏你的代碼?你需要做的是跳過不適合有效範圍的字符,或者捕獲異常。 –

+0

將您的代碼粘貼到這裏 – curiousguy

+0

該頁面的文本編碼爲iso-8859-1,但meta標頭顯示爲utf8,這會使瀏覽器無法識別該字符,如果將編碼更改爲iso-8859-1將會能夠轉換爲unicode那個字符 – markcial

回答

0

的代碼還不清楚,但我認爲問題是與解碼,它應該解碼爲UTF-8

例如工作後,

string = '(10 meters) Size: 3.59� (H) x 1.98� (W) x 0.52� (D) Weight:'.decode('utf-8')