2016-04-24 121 views
-1

尋找一種方式來解決UnicodeEncodeError:「ASCII」編解碼器不能在4位編碼字符U「\ u2019」:有序不在範圍內(128)機器人框架ASCII碼編碼

目前我的代碼是:

coding: utf-8 
*** Settings *** 
Documentation This is a simple test with Robot Framework 
Suite Setup  Connect To Database pymysql ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort} 
Suite Teardown Disconnect From Database 
Library   Selenium2Library 
Library   DatabaseLibrary 
Library   OperatingSystem 
Library   String 

*** Variables *** 
${DBHost}   localhost 
${DBName}   robottest 
${DBPass}   sahill 
${DBPort}   3306 
${DBUser}   sahill 

*** Test Cases *** 
Open Browser To Start Roboting 
    [Documentation] Sarting robot 
    [Tags] Smoke 
    Open Browser http://movieplus.cc/a-z-movies/ firefox 
    Maximize Browser Window 
    WAIT UNTIL PAGE CONTAINS have any legal issues please contact appropriate media file owners/hosters. 

    Click Element xpath=//div[@class='wrapper']//div[@class='thumbs']//div[@class='wrap']//div[1]//a 
    WAIT UNTIL PAGE CONTAINS have any legal issues please contact appropriate media file owners/hosters. 
    ${count} = Get Matching Xpath Count  xpath=//div[@class='wrap_content']//ul[@class='list']//li 
    ${title} = get text xpath=//div[@class='wrap_content']//h2[@class='title']//span[@class='color'] 
    ${cate} = get text xpath=//div[@class='wrap_content']//ul[@class='list'] 
    ${director} = get text xpath=//div[@class='wrap_content']//div[1]//span[@class='desc'] 
    ${cast} = get text xpath=//div[@class='wrap_content']//div[2]//span[@class='desc'] 
    ${content} = get text xpath=//div[@class='wrap_content']//div[3]//span[@class='description']//p 
    ${image_link} =  Get element attribute xpath=//div[@class='content']//div[@class='wrap_img']//[email protected] 
    Execute SQL String  INSERT INTO test VALUES('${title}', '${cate}', '${director}', '${cast}','${content}', '${image_link}', 'link'); 
    Go Back 

${content}變量此內容商店

A young woman wakes up in the basement of a man who claims he saved her life after pulling her from her overturned car which violently crashed along the highway. The man states that the world above them is no longer safe and is now a danger zone that threatens the rest of humanity. With no knowledge of what actually lies above and trust running thin between them, the woman questions **what’s true and what’s not**. Is the man a delusional psychotic that has a more sinister agenda for her? Or is it really hell on earth above ground like he swears? 
+1

你使用python 2還是python 3? – user312016

+0

Python 2,與python 3機器人框架不工作 – Sahill

+0

我編輯我的答案。 – user312016

回答

0

您正在使用python 2,因此,你的問題是,你有沒有在你的文件的開頭定義一個適當的編碼。

# coding: utf-8 

你可以選擇:

  • 把編碼頭在你的文件
  • 或頂部:

    Use an ascii character for "’", like "'" 
    
+0

感謝您的回覆,基本上我使用機器人框架獲取內容的系統從網站獲取文本關鍵字,在這種情況下我無法更改「'」 – Sahill

+0

@Sahill This是不是我在你的代碼中看到的,你能否更新它來提供一個類似於現實世界的例子? – user312016

+0

親愛的主席先生,我更新了我的問題,現在你看到我的所有代碼 – Sahill

0

執行SQL字符串之前,您可以編碼內容,如:

${content} = Encode String To Bytes ${content} ASCII errors=replace 

在這種情況下,結果是:... the woman questions what?s true and what?s not. Is the ...

如果忽略錯誤:

${content} = Encode String To Bytes ${content} ASCII errors=ignore 

結果將是:... the woman questions whats true and whats not. Is the ...

(這將是更容易理解)