我正在使用BeautifulSoup和urllib制作维基百科网络刮刀。我只是不断得到同样令人讨厌的错误。
我的代码:
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'
答案 0 :(得分:0)
nickname
的数据类型为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)