Python 'latin-1' 编解码器无法编码字符 - 如何忽略字符?
Python 'latin-1' codec can't encode character - How to ignore characters?
这是我的代码的要点。它试图从旧网站获取一些文本。这不是我的,所以我无法更改来源。
from bs4 import BeautifulSoup
import requests
response = requests.get("https://mattgemmell.com/network-link-conditioner-in-lion/")
data = response.text
soup = BeautifulSoup(data, 'lxml')
article = soup.find_all('article')[0]
text = article.find_all('p')[1].text
print(text)
给出这个:
'If youâ\x80\x99re a developer of either Mac or iOS apps that use networking, thereâ\x80\x99s a new feature in the Developer Tools for Mac OS X 10.7 â\x80\x9cLionâ\x80\x9d (read my review of it at The Guardian) which will be useful to you. This brief article describes how it works.'
我可以用它来转换像 â\x80\x99:
这样的部分
converted_text = bytes(text, 'latin-1').decode('utf-8')
确实有效。
但是如果你得到文本的不同部分:
text = article.find_all('p')[8].text
给我:
'\n← 在 Lion\nUsing 上的文本中查找模式 OS X Lion 上的空格 →\n'
使用 bytes(text, 'latin-1')
得到:
'latin-1' 编解码器无法对位置 1 中的字符“\u2190”进行编码:序号不在范围内 (256)
我猜是箭头?我怎样才能做到所有非拉丁字符都被自动忽略和丢弃。
任何想法都会很有帮助!
使用bytes
的第三个参数告诉它如何处理错误:
converted_text = bytes(text, 'latin-1', 'ignore')
^^^^^^
你会丢失箭头,但其他一切都完好无损:
>>> text = '\n← Find Patterns in text on Lion\nUsing Spaces on OS X Lion →\n'
>>> converted_text = bytes(text, 'latin-1', 'ignore')
>>> converted_text
'\n Find Patterns in text on Lion\nUsing Spaces on OS X Lion \n'
这里有关于文档中参数的更多信息 - https://docs.python.org/3.3/howto/unicode.html:
The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), or 'ignore' (just leave the character out of the Unicode result).
您不想忽略这些字符。它们是您收到的数据已使用错误的字符编码解码的症状。在你的情况下 requests
错误地猜测编码是 latin-1
。真正的编码是 utf-8
并在 HTML 响应的 <meta>
标签中指定。 requests
是一个用于处理 HTTP 的库,它不知道 HTML。由于 Content-Type
header 没有指定编码 requests
求助于猜测编码。然而,BeautifulSoup
是一个用于处理 HTML 的库,它非常擅长检测编码。因此,您希望从响应中获取原始字节并将其传递给 BeautifulSoup
。即
from bs4 import BeautifulSoup
import requests
response = requests.get("https://mattgemmell.com/network-link-conditioner-in-lion/")
data = response.content # we now get `content` rather than `text`
assert type(data) is bytes
soup = BeautifulSoup(data, 'lxml')
article = soup.find_all('article')[0]
text = article.find_all('p')[1].text
print(text)
assert type(text) is str
assert 'Mac OS X 10.7 “Lion”' in text
这是我的代码的要点。它试图从旧网站获取一些文本。这不是我的,所以我无法更改来源。
from bs4 import BeautifulSoup
import requests
response = requests.get("https://mattgemmell.com/network-link-conditioner-in-lion/")
data = response.text
soup = BeautifulSoup(data, 'lxml')
article = soup.find_all('article')[0]
text = article.find_all('p')[1].text
print(text)
给出这个:
'If youâ\x80\x99re a developer of either Mac or iOS apps that use networking, thereâ\x80\x99s a new feature in the Developer Tools for Mac OS X 10.7 â\x80\x9cLionâ\x80\x9d (read my review of it at The Guardian) which will be useful to you. This brief article describes how it works.'
我可以用它来转换像 â\x80\x99:
这样的部分converted_text = bytes(text, 'latin-1').decode('utf-8')
确实有效。
但是如果你得到文本的不同部分:
text = article.find_all('p')[8].text
给我:
'\n← 在 Lion\nUsing 上的文本中查找模式 OS X Lion 上的空格 →\n'
使用 bytes(text, 'latin-1')
得到:
'latin-1' 编解码器无法对位置 1 中的字符“\u2190”进行编码:序号不在范围内 (256)
我猜是箭头?我怎样才能做到所有非拉丁字符都被自动忽略和丢弃。
任何想法都会很有帮助!
使用bytes
的第三个参数告诉它如何处理错误:
converted_text = bytes(text, 'latin-1', 'ignore')
^^^^^^
你会丢失箭头,但其他一切都完好无损:
>>> text = '\n← Find Patterns in text on Lion\nUsing Spaces on OS X Lion →\n'
>>> converted_text = bytes(text, 'latin-1', 'ignore')
>>> converted_text
'\n Find Patterns in text on Lion\nUsing Spaces on OS X Lion \n'
这里有关于文档中参数的更多信息 - https://docs.python.org/3.3/howto/unicode.html:
The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), or 'ignore' (just leave the character out of the Unicode result).
您不想忽略这些字符。它们是您收到的数据已使用错误的字符编码解码的症状。在你的情况下 requests
错误地猜测编码是 latin-1
。真正的编码是 utf-8
并在 HTML 响应的 <meta>
标签中指定。 requests
是一个用于处理 HTTP 的库,它不知道 HTML。由于 Content-Type
header 没有指定编码 requests
求助于猜测编码。然而,BeautifulSoup
是一个用于处理 HTML 的库,它非常擅长检测编码。因此,您希望从响应中获取原始字节并将其传递给 BeautifulSoup
。即
from bs4 import BeautifulSoup
import requests
response = requests.get("https://mattgemmell.com/network-link-conditioner-in-lion/")
data = response.content # we now get `content` rather than `text`
assert type(data) is bytes
soup = BeautifulSoup(data, 'lxml')
article = soup.find_all('article')[0]
text = article.find_all('p')[1].text
print(text)
assert type(text) is str
assert 'Mac OS X 10.7 “Lion”' in text