I'm using BeautifulSoup and urllib to make a Wikipedia web scraper. I just keep getting the same annoying error.
My code:
from bs4 import BeautifulSoup
import urllib
page = urllib.urlopen("https://en.wikipedia.org/wiki/Donald_Trump").read()
soup = BeautifulSoup(page, "html.parser")
nickname = soup.find_all("span", class_="nickname")
nickname.replace('[<span class="nickname">','')
nickname.replace('</span>]','')
print(nickname)
The error:
AttributeError: 'ResultSet' object has no attribute 'replace'
nickname
has datatype ResultSet
, you have to convert it to a string if you want to do some string operations on it.
from bs4 import BeautifulSoup
import urllib
page = urllib.urlopen("https://en.wikipedia.org/wiki/Donald_Trump").read()
soup = BeautifulSoup(page, "html.parser")
nickname = soup.find_all("span", class_="nickname")
nicknameStr = str(nickname)
nicknameStr.replace('[<span class="nickname">','')
nicknameStr.replace('</span>]','')
print(nicknameStr)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.