美体编码.字节没有名为find\u all的属性?

2024-04-19 08:54:18 发布

您现在位置:Python中文网/ 问答频道 /正文

 `self.urlOpen=urllib.request.urlopen("http://facebook.com")
  self.content=self.urlOpen.read()
  soup=BeautifulSoup(self.content,"html5lib")
  self.links=soup.find_all("a")`

'charmap' codec can't encode characters in position....

所以当我试图对soup变量进行编码时 self.urlOpen=urllib.request.urlopen("http://facebook.com") self.content=self.urlOpen.read() soup=BeautifulSoup(self.content,"html5lib") soup=soup.encode("utf-8") self.links=soup.find_all("a")

'bytes' object has no attribute called find_all

我试过了 self.urlOpen=urllib.request.urlopen("http://facebook.com") self.content=self.urlOpen.read() soup=BeautifulSoup(self.content.decode("utf-8","ignore"),"html5lib") self.links=soup.find_all("a")

但同样的错误也发生了

那我该怎么编码呢?在


Tags: selfcomhttpreadfacebookrequestcontentall
1条回答
网友
1楼 · 发布于 2024-04-19 08:54:18

问题是什么?
find_all不应该抛出编码错误,也不应该对bs4.BeautifulSoup对象调用encode,因为encode返回的是bytestring,而不是soup!-所以你不能对它调用find_all。在

您在任何地方使用soup.prettify()吗?在这种情况下,这可能是抛出错误的行。请包含代码的Minimal, Complete and Verifiable example。在

相关问题 更多 >