在删除此站点时遇到问题

2024-10-01 22:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在这里刮一个网站在工作,我有一个错误代码的问题。这是我的密码

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:“非类型”对象不可编辑

如有任何建议,将不胜感激

enter image description here


Tags: textdivboxif网站htmlfindattrs
1条回答
网友
1楼 · 发布于 2024-10-01 22:38:46

如果仔细查看html,'class':'box-bottom'只出现一次,这也是为什么错误会说“object is not iterable”,这基本上意味着targetDiv不是可以循环的数组

试试看

print(targetdivs) 

之后

targetdivs=rootDiv.find('div',attrs={'class':'box-bottom'})

然后你会得到你的答案,希望这有帮助

相关问题 更多 >

    热门问题