【发布时间】:2012-05-04 19:31:41
【问题描述】:
import urllib.request as u
zipcode = str(47401)
url = 'http://watchdog.net/us/?zip=' + zipcode
con = u.urlopen(url)
page = str(con.read())
value3 = int(page.find("<title>")) + 7
value4 = int(page.find("</title>")) - 15
district = str(page[value3:value4])
print(district)
newdistrict = district.replace("\xe2\x80\x99","'")
print(newdistrict)
出于某种原因,我的代码以以下格式提取标题:IN-09: Indiana\xe2\x80\x99s 9th。我知道\xe 字符串是' 符号的unicode,但我不知道如何让python 用' 符号替换那组字符。我试过解码字符串,但它已经是 unicode 并且上面的替换代码没有改变任何东西。关于我做错了什么有什么建议吗?
【问题讨论】:
-
你试过使用
unicode字面量吗? -
我不完全确定你的意思,你能提供更多信息吗?
-
不是
',而是’(U+2019,右单引号)。 -
看到你正在使用
urllib.request,我想你正在使用 Python 3。 -
是的,使用 Python 3在区域变量中找到该字符串,即使在调用 print 函数时它正在屏幕上打印它。
标签: python html utf-8 python-3.x