我试图在这里刮一个网站在工作,我有一个错误代码的问题。这是我的密码
from bs4 import BeautifulSoup
html = "https://reddit.com" #just an example
soup = BeautifulSoup(requests.get(html).text, 'html.parser')
rootDiv=soup.find('div',attrs={'id':'root'})
if(rootDiv):
targetdivs=rootDiv.find('div',attrs={'class':'box-bottom'})
for targetDiv in targetdivs:
span=targetdivs.find('span',attrs={'class':'box-bottom-offline'})
if(span):
print(span.text)
这是我要刮的东西的剪报,用红色圈起来
运行代码后,出现错误:
TypeError:“非类型”对象不可编辑
如有任何建议,将不胜感激
如果仔细查看html,'class':'box-bottom'只出现一次,这也是为什么错误会说“object is not iterable”,这基本上意味着targetDiv不是可以循环的数组
试试看
之后
然后你会得到你的答案,希望这有帮助
相关问题 更多 >
编程相关推荐