我正在使用BeautifulSoup和Urllib来制作Wikipedia Web刮板。我只是继续遇到同样令人讨厌的错误。
我的代码:
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)
错误:
AttributeError: 'ResultSet' object has no attribute 'replace'
nickname
具有datatype ResultSet
,如果要对其进行一些字符串操作,则必须将其转换为字符串。
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)