首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有str和字节码对象的Python3错误

带有str和字节码对象的Python3错误
EN

Stack Overflow用户
提问于 2018-12-11 16:21:52
回答 2查看 93关注 0票数 0

因此,我尝试编写一个简单的函数来清理文本并对其进行总结:

代码语言:javascript
运行
复制
def getTextWaPo(url):
page = urllib2.urlopen(url).read().decode('utf8')
soup = BeautifulSoup(page, "lxml")
text = ' '.join(map(lambda p: p.text, soup.find_all('article')))
return text.encode('ascii', errors='replace').replace("?"," ")

但是对于这段代码,我得到了以下错误:

代码语言:javascript
运行
复制
  File "Autosummarizer.py", line 12, in getTextWaPo
  return text.encode('ascii', errors='replace').replace("?"," ")
  TypeError: a bytes-like object is required, not 'str'

  line 12 ==> text = getTextWaPo(articleURL)

我该怎么办?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-11 18:37:36

您正在对第12行中的数据进行编码,您必须使用字节。as replace(b"?", b" ")

代码看起来就像

代码语言:javascript
运行
复制
import requests
from urllib.request import urlopen
from bs4 import BeautifulSoup
def getTextWaPo(url):
    page = urlopen(url).read().decode('utf8')
    soup = BeautifulSoup(page, "lxml")
    text = ' '.join(map(lambda p: p.text, soup.find_all('article')))
    return text.encode('ascii', errors='replace').replace(b"?",b" ")
getTextWaPo("https://stackoverflow.com/")
票数 0
EN

Stack Overflow用户

发布于 2018-12-11 18:31:33

您必须将最后一行return text.encode('ascii', errors='replace').replace("?"," ")更改为return text.encode('ascii', errors='replace').replace(b"?", b" "),因为在encode()之后,您要在bytes上操作,并且必须用其他字节替换字节。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53728305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档