hashka92
hashka92

Reputation: 29

Python3 error with str and byte code object

So I tried to write a simple function to clean a text and summarize it:

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("?"," ")

but for this piece of code I get this error :

  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)

what should I do?

Upvotes: 0

Views: 155

Answers (2)

Md Jewele Islam
Md Jewele Islam

Reputation: 1037

Your are encoding data in line 12 using you have to use bytes. as replace(b"?", b" ")

The code look like

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/")

Upvotes: 0

Volker Böhm
Volker Böhm

Reputation: 21

You must change your last line return text.encode('ascii', errors='replace').replace("?"," ") to return text.encode('ascii', errors='replace').replace(b"?", b" ") because after the encode() you're operating on bytes, and must replace bytes with other bytes.

Upvotes: 0

Related Questions