我想连接并接收来自特定网站链接的http响应。 我有很多Python代码:
import urllib.request
import os,sys,re,datetime
fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()
mystr = mybytes.decode(encoding=sys.stdout.encoding)
fp.close()
当我将响应作为参数传递给:
BeautifulSoup(str(mystr), 'html.parser')
要获取已清理的html文本,我遇到以下错误:
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u25bc' in position 1139: character maps to <undefined>.
问题是我怎样才能解决这个问题?你知道吗
完整代码:
import urllib.request
import os,sys,re,datetime
fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()
mystr = mybytes.decode(encoding=sys.stdout.encoding)
fp.close()
from bs4 import BeautifulSoup
soup = BeautifulSoup(str(mystr), 'html.parser')
mystr = soup;
print(mystr.get_text())
如果您使用
requests
库,您可以避免以下复杂情况:)BeautifulSoup非常乐意使用
urlopen
返回的类似文件的对象:相关问题 更多 >
编程相关推荐