可能重複:
Convert XML/HTML Entities into Unicode String in Python我該如何擺脫'這樣的字符而不是撇號?
我試圖刮使用Python網站。我導入並使用urllib2,BeautifulSoup和re模塊。
response = urllib2.urlopen(url)
soup = BeautifulSoup(response)
responseString = str(soup)
coarseExpression = re.compile('<div class="sodatext">[\n]*.*[\n]*</div>')
coarseResult = coarseExpression.findall(responseString)
fineExpression = re.compile('<[^>]*>')
fineResult = []
for coarse in coarseResult:
fine = fineExpression.sub('', coarse)
#print(fine)
fineResult.append(fine)
不幸的是,像撇號的字符出現在損壞的方式,像這樣 - &#X27; 有沒有辦法避免這種情況?或者一種方法來輕鬆替換它們?
那不是破壞,這對一個單引號的HTML/XML字符實體(HTTP://en.wikipedia .ORG /維基/ List_of_XML_and_HTML_character_entity_references)。你總是可以將這些實體解碼回到它們的ASCII等價物。 (http://stackoverflow.com/questions/57708/convert-xml-html-entities-into-unicode-string-in-python) – dreynold 2011-12-22 18:00:38
您正在加載一個頁面在BeautifulSoup **只是爲了REGEX IT!?**爲什麼你在做這個可怕的,可怕的事情嗎?! – 2011-12-22 18:12:26
@FrancisAvila,我仍然感覺我圍繞着Python。你能告訴我更好的方法嗎? – nindalf 2011-12-24 14:15:08