2012-09-28 9 views
5

Mam problemy z wyświetlania treści, mój program:Zmień & # 39 do normalnego charakteru

#! /usr/bin/python 

import urllib 
import re 

url = "http://yahoo.com" 
pattern = '''<span class="medium item-label".*?>(.*)</span>''' 

website = urllib.urlopen(url) 
pageContent = website.read() 
result = re.findall(pattern, pageContent) 

for record in result: 
    print record 

wyjściowa:

Masked teen killed by dad 
First look in &#39;Hotel of Doom&#39; 
Ex-NFL QB&#39;s sad condition 
Reporter ignores warning 
Romney&#39;s low bar for debates 

Więc pytanie, co należy uwzględnić w kodzie w aby przekształcić & nr 39 w postaci

+0

może powielony z http://stackoverflow.com/questions/57708/convert-xml-html-entities-into-unicode- string-in-python – charlee

Odpowiedz

8

W python2:

In [16]: text = 'Ex-NFL QB&#39;s sad condition' 

In [17]: import HTMLParser 

In [18]: parser = HTMLParser.HTMLParser() 

In [19]: parser.unescape(text) 
Out[19]: u"Ex-NFL QB's sad condition" 

W Python3:

import html.parser as htmlparser 
parser = htmlparser.HTMLParser() 
parser.unescape(text) 
+0

Działa dobrze! Dziękuję Ci bardzo – Vor

0

w JavaScript:

text = text.replace(/&#39;/g,"'"); 
Powiązane problemy