使用Beautiful Soup解析HTML时的UnicodeEncodeError

时间:2018-05-27 19:01:00

标签: python-3.x netbeans beautifulsoup

我正在学习如何使用Beautiful Soup解析HTML。我在虚拟环境(Python 3.6)中一直使用NetBeans作为IDE,我没有遇到任何问题。

我感到惊讶的是,当我尝试一个非常简单的代码以便从网站上找到所有'a'元素时,我收到了一个错误。如果我尝试选择所有'h3'或任何其他元素,但是当我尝试使用'a'时不会。

import requests
import bs4
url = 'https://www.python.org/events/'
req = requests.get(url)
req.raise_for_status()
reqText = req.text
soup = bs4.BeautifulSoup(reqText, "html.parser")
selection1 = soup.find_all('a')
print(selection1)

错误说:

Traceback (most recent call last):
  File "C:\PROJECTS\Python36\src\new_main.py", line 9, in <module>
    print(selection)
  File "C:\Anaconda2\envs\env1a\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u25bc' in position 211: character maps to <undefined>

我安装了另一个IDE(Spyder),并且在使用它时它起作用了。

看起来这是编解码器的问题。有谁能告诉我如何解决这个问题。为什么Netbeans在选择一个非常具体的元素时失败,否则它会起作用。

0 个答案:

没有答案