0

我正在做一个知识工程项目。

当我在爬一些科学家的个人网站时,出现了这个错误。

import html2text
import requests
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import urllib


homepage = "http://angom.myweb.cs.uwindsor.ca"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=homepage, headers=headers)
print(req)
c = urlopen(req).read()
print(type(c))

content = urlopen(req).read().decode("utf-8")

UnicodeDecodeError:“utf-8”编解码器无法解码位置 139604 中的字节 0xf6:无效的起始字节

4

1 回答 1

0

页眉中的编码说明:

<meta http-equiv=Content-Type content="text/html; charset=windows-1252">

..所以在解码字符串时使用它。

content = urlopen(req).read().decode("windows-1252")

将在这种情况下工作。

如果您打算使用 BeautifulSoup,它已经很好地解决了编码问题

于 2017-07-18T04:20:28.557 回答