【问题标题】:Python and string accentsPython 和字符串重音符号
【发布时间】:2016-07-01 16:04:22
【问题描述】:

我正在制作一个网络爬虫。
我访问谷歌搜索,获得网页链接,然后获得<title> 标签的内容。
问题是,例如,字符串"P\xe1gina N\xe3o Encontrada!" 应该是"Página Não Encontrada!"。 我尝试解码为 latin-1,然后编码为 utf-8,但它不起作用。

    r2 = requests.get(item_str)
    texto_pagina = r2.text
    soup_item = BeautifulSoup(texto_pagina,"html.parser")
    empresa = soup_item.find_all("title")
    print(empresa_str.decode('latin1').encode('utf8'))

你能帮帮我吗? 谢谢!

【问题讨论】:

  • 也许这里有一些答案link
  • 没用。我已经试过了...谢谢
  • 你能告诉我们 print([empresa]) 的结果吗?所以我们可以准确地看到当前编码是什么。那是python3吗?
  • print(empresa_str) : [Ops...P\xe1gina N\xe3o Entrada!] [ANADI Consultoria ERP Totvs] [专家 | Consultoria Microsiga Protheus homologada e certificada Totvs - Home] [Consultoria TOTVS\xae | ALFA Sistemas de Gest\xe3o] [.: TOTVS IV2 - Tecnologia e Sistemas :.] [Consultoria TOTVS Protheus] [CONSULTORIA TOTVS PROTHEUS |系统]
  • 代替print(empresa_str),你能按照@YOU上面的建议做吗,即:print([empresa])

标签: python utf-8 latin1


【解决方案1】:

您可以将检索到的文本变量更改为:

string = u'P\xe1gina N\xe3o Encontrada!'.encode('utf-8')

在打印string 之后,它对我来说似乎工作得很好。


编辑

您是否尝试过仅使用empresa_str.decode('latin1') 而不是添加.encode('utf8')

如:

string = empresa_str.decode('latin_1')

【讨论】:

  • 但是'P\xe1gina N\xe3o Entrada!'在变量内部。如何将您的语法与变量 (empresa_str) 一起使用?
  • 如果您按照我在上面的答案中编辑的内容进行操作会怎样?
  • 相同的结果:P\xe1gina N\xe3o Entrada!当我打印它时
【解决方案2】:

不是最优雅的解决方案,但对我有用:

def remove_all(substr, str):
 index = 0
 length = len(substr)
 while string.find(str, substr) != -1:
    index = string.find(str, substr)
    str = str[0:index] + str[index+length:]
 return str

 def latin1_to_ascii (unicrap):
    xlate={ 'xc3cb3':'o' , 'xc3xa7':'c','xc3xb5':'o',  'xc3xa3':'a',  'xc3xa9':'e',
    'xc0':'A', 'xc1':'A', 'xc2':'A', 'xc3':'A', 'xc4':'A', 'xc5':'A',
    'xc6':'Ae', 'xc7':'C',
    'xc8':'E', 'xc9':'E', 'xca':'E', 'xcb':'E',
    'xcc':'I', 'xcd':'I', 'xce':'I', 'xcf':'I',
    'xd0':'Th', 'xd1':'N',
    'xd2':'O', 'xd3':'O', 'xd4':'O', 'xd5':'O', 'xd6':'O', 'xd8':'O',
    'xd9':'U', 'xda':'U', 'xdb':'U', 'xdc':'U',
    'xdd':'Y', 'xde':'th', 'xdf':'ss',
    'xe0':'a', 'xe1':'a', 'xe2':'a', 'xe3':'a', 'xe4':'a', 'xe5':'a',
    'xe6':'ae', 'xe7':'c',
    'xe8':'e', 'xe9':'e', 'xea':'e', 'xeb':'e',
    'xec':'i', 'xed':'i', 'xee':'i', 'xef':'i',
    'xf0':'th', 'xf1':'n',
    'xf2':'o', 'xf3':'o', 'xf4':'o', 'xf5':'o', 'xf6':'o', 'xf8':'o',
    'xf9':'u', 'xfa':'u', 'xfb':'u', 'xfc':'u',
    'xfd':'y', 'xfe':'th', 'xff':'y',
    'xa1':'!', 'xa2':'{cent}', 'xa3':'{pound}', 'xa4':'{currency}',
    'xa5':'{yen}', 'xa6':'|', 'xa7':'{section}', 'xa8':'{umlaut}',
    'xa9':'{C}', 'xaa':'{^a}', 'xab':'<<', 'xac':'{not}',
    'xad':'-', 'xae':'{R}', 'xaf':'_', 'xb0':'{degrees}',
    'xb1':'{+/-}', 'xb2':'{^2}', 'xb3':'{^3}', 'xb4':'',
    'xb5':'{micro}', 'xb6':'{paragraph}', 'xb7':'*', 'xb8':'{cedilla}',
    'xb9':'{^1}', 'xba':'{^o}', 'xbb':'>>', 
    'xbc':'{1/4}', 'xbd':'{1/2}', 'xbe':'{3/4}', 'xbf':'?',
    'xd7':'*', 'xf7':'/'
    }
    unicrap = remove_all ('\\', unicrap)
    unicrap = remove_all('&amp;', unicrap)
    unicrap = remove_all('u2013', unicrap)

    r = unicrap
    for item,valor in xlate.items():
        #print item, unicrap.find(item)
        r = r.replace(item,valor)
    return r

【讨论】:

  • 这完全去除了重音符号和其他变音符号,而不是显示原始值。另外,不要命名变量str,也不需要string模块来执行find; Python 的str 几十年来一直采用find 方法,所以string.find(str, substr) 只是说str.find(substr) 的冗长/缓慢的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 2012-01-21
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多