Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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
Unicode编码错误:Charmap无法在Python中编码字符\xa9_Python_Python 3.x_Web Scraping_Decode_Html Encode - Fatal编程技术网

Unicode编码错误:Charmap无法在Python中编码字符\xa9

Unicode编码错误:Charmap无法在Python中编码字符\xa9,python,python-3.x,web-scraping,decode,html-encode,Python,Python 3.x,Web Scraping,Decode,Html Encode,嗨,我正在写剪贴代码,但当我试图从网站上获得所有段落时,它会给我以下错误 Unicode编码错误:Charmap无法编码字符“\xa9” 这是我的密码: #加载库 导入URL库 从urllib.parse导入urlparse 从urllib.parse导入urljoin 导入urllib.request 从bs4导入BeautifulSoup #定义用于抓取的URL 新闻URL=”http://www.techspot.com/news/67832-netflix-exceeds-growth

嗨,我正在写剪贴代码,但当我试图从网站上获得所有段落时,它会给我以下错误 Unicode编码错误:Charmap无法编码字符“\xa9”

这是我的密码:

#加载库
导入URL库
从urllib.parse导入urlparse
从urllib.parse导入urljoin
导入urllib.request
从bs4导入BeautifulSoup
#定义用于抓取的URL
新闻URL=”http://www.techspot.com/news/67832-netflix-exceeds-growth-expectations-home-abroad-stock-soars.html"
thepage=urllib.request.urlopen(新闻URL)
soup=BeautifulSoup(页面“html.parser”)
article=soup.find_all('div',{'class','articleBody'})
对于第条中的pg:
段落=soup.findAll('p')
ptag=段落
print(ptag)
soup.findAll()返回一个ResultSet对象,它基本上是一个没有属性encode的列表。您打算使用.text来代替:

text = soup.text
或“加入”文本:

text = "".join(soup.findAll(whatever, you, want))

不要尝试打印到控制台,因为控制台无法显示unicode。例如,使用utf-8编码写入文件。而且
ptag
不是字符串,因此没有编码方法。您可以尝试
str(ptag).encode('ascii','ignore')
.BTW。将unicode打印到控制台很可能已经解决了这个问题。可能是重复的。fidn如何在控制台中设置utf-8(cp65001)。@mkiever感谢您提供的解决方案:*