Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何转换这样的字符;a³;a¡;a´;a§&引用;在unicode中,使用python?_Python_String_Unicode_Utf 8_Urllib - Fatal编程技术网

如何转换这样的字符;a³;a¡;a´;a§&引用;在unicode中,使用python?

如何转换这样的字符;a³;a¡;a´;a§&引用;在unicode中,使用python?,python,string,unicode,utf-8,urllib,Python,String,Unicode,Utf 8,Urllib,我正在制作一个爬虫来获取里面的文本html,我正在使用beautifulsoup 当我使用urllib2打开url时,该库会自动将使用葡萄牙语口音(如“ãóéõ”)的html转换为其他字符(如“a³a¨a¨a§)” 我想要的是不带口音的单词 contrãrio->contrario 我试着使用这个算法,但是当文本使用像“olácoração contrário”这样的词时,这个算法就起作用了 你有字节数据。您需要Unicode数据。图书馆不是应该为你解码吗?必须这样做,因为您没有HTTP头,因此

我正在制作一个爬虫来获取里面的文本html,我正在使用beautifulsoup

当我使用urllib2打开url时,该库会自动将使用葡萄牙语口音(如“ãóéõ”)的html转换为其他字符(如“a³a¨a¨a§)”

我想要的是不带口音的单词

contrãrio->contrario

我试着使用这个算法,但是当文本使用像“olácoração contrário”这样的词时,这个算法就起作用了


你有字节数据。您需要Unicode数据。图书馆不是应该为你解码吗?必须这样做,因为您没有HTTP头,因此缺少编码

编辑 这听起来很奇怪,但Python似乎不支持其web库中的内容解码。如果运行此程序:

#!/usr/bin/env python    
import re
import urllib.request
import io
import sys

for s in ("stdin","stdout","stderr"):
    setattr(sys, s, io.TextIOWrapper(getattr(sys, s).detach(), encoding="utf8"))

print("Seeking r\xe9sum\xe9s")

response = urllib.request.urlopen('http://nytimes.com/')
content  = response.read()

match    = re.search(".*r\xe9sum\xe9.*", content, re.I | re.U)
if match:
    print("success: " + match.group(0))
else:
    print("failure")
您将得到以下结果:

Seeking résumés
Traceback (most recent call last):
  File "ur.py", line 16, in <module>
    match    = re.search(".*r\xe9sum\xe9.*", content, re.I | re.U)
  File "/usr/local/lib/python3.2/re.py", line 158, in search
    return _compile(pattern, flags).search(string)
TypeError: can't use a string pattern on a bytes-like object
当尽职尽责地运行时,会产生:

Seeking résumés
search success: <li><a href="http://hiring.nytimes.monster.com/products/resumeproducts.aspx">Search Résumés</a></li>

只是想一想。

首先,你必须确保你的爬虫程序返回的HTML是unicode文本(例如,Scrapy有一个方法响应。body作为unicode()正好执行此操作)

一旦您有了无法理解的unicode文本,从unicode文本到等效ascii文本的步骤就在这里-


输出为“Beijing”

您的问题是自动转换不正确,还是希望将全重音字符减少到最接近的ASCII等效字符?还是两者兼而有之?问题中显示的规范化技巧是消除重音的好方法。唯一的问题一定是转换不起作用——如果OP对输出的呈现有点近似,问题可能是数据是UTF-8,但被解释为拉丁语-1。我意识到,对于其他网站,我所做的恰恰解决了这个问题。问题是这个网站。听起来很奇怪,因为当我看到网页的源代码时,我看到的是另一个。在对其进行正则表达式搜索之前,您是否尝试过使用content.decode(“utf-8”)将检索到的内容转换为字符串?如果您执行此搜索(“%r\xe9sum\xe9.*”、content.decode(“utf-8”)、re.I | re.U),它会工作fine@wberry:我完全知道我在做什么:你没有读到“那意味着
。read()
返回的是原始字节,而不是真正的字符串。”???关键是Python类中没有
decoded_content
方法可以像Perl类中那样生成正确解码的字符串。应该有。让用户自己做这件事是不可接受的。如果HTTP只被认为能够传递字符数据,那将是“完全站不住脚的”。但是HTTP同样可以轻松地传递诸如图像之类的字节流,因此
urllib
API为您提供字节。Beautifulsoup套餐旨在弥补这一差距。请不要急于对技术做出判断,除非这个问题会引起这样的问题。@tchrist无论python和perl之间有什么区别,以及您认为perl有多方便都无关紧要。这是一个关于python而不是perl的问题,这种类型的讨论应该在其他地方进行。@tchrist如果您看到我的第一条评论,您将看到答案。我从来没有说过python是完美的,也没有说过答案是错误的,您只是继续谈论perl中的python有多方便,在我看来,perl并不是这里所要求的。
#!/usr/bin/env perl    
use strict;
use warnings;    
use LWP::UserAgent;

binmode(STDOUT, "utf8");    
print("Seeking r\xe9sum\xe9s\n");

my $agent = LWP::UserAgent->new();
my $response = $agent->get("http://nytimes.com/");

if ($response->is_success) {
    my $content = $response->decoded_content;
    if ($content =~ /.*r\xe9sum\xe9.*/i) {
        print("search success: $&\n");
    } else {
        print("search failure\n");
    } 
} else {
    print "request failed: ", $response->status_line, "\n";
} 
Seeking résumés
search success: <li><a href="http://hiring.nytimes.monster.com/products/resumeproducts.aspx">Search Résumés</a></li>
 use Unicode::Normalize;
 ($unaccented = NFD($original)) =~ s/\pM//g;
from unidecode import unidecode
print unidecode(u"\u5317\u4EB0")