Beautifulsoup extracking属性类型错误:“NoneType”对象不是可迭代
问题描述:
我用来获取标记该Beautifulsoup extracking属性类型错误:“NoneType”对象不是可迭代
for a in soup.find_all('img', {'data-event': 'Clicked image'},
src=True,alt=True):
itemobj = a['src'] + ' --- ' + a['alt']
属性现在我在其他网站上工作,当我尝试这样扔类型错误:“NoneType”对象是不是可迭代
song_link = line.find('td').find('a')['href'] (This works well)
sss = line.find('span')['title'] (This in not working. But when I delete ['title'] part it works and shows inside of the <span> tag
我的数据:
<span class="rating" title="4.5">
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__half"></span>
</span>
我一直在寻找解决THI但到目前为止,他们都没有为我工作。
答
当我在你提供的数据上试过你的代码时,它对我来说工作得很好,所以我将假设有更多的数据。
soup.find('span')['title']
用“span”检查它找到的第一个东西,如果它不包含标题标签,则会引发异常。
例如on
<span></span>
<span class="rating" title="4.5">
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__active"></span>
<span class="icon-rating-sm icon-rating-sm__half"></span>
</span>
该代码不起作用。
至少这是我发生了几次。
你能说清楚你的问题吗?你在'line'变量中存储了什么?这是你发布的数据吗? –