使用findall获取bs4元素python的标题子级?
我正在使用find_all方法获取一些元素,如下所示:使用findall获取bs4元素python的标题子级?,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我正在使用find_all方法获取一些元素,如下所示: elements = soup.find_all('a', {'class': "watchlink"}) 要获得如下元素: <a class="watchlink" href="https://www1.swatchseries.to/freecale.html?r=iexyJrjdCI6InRuT0tnVpFR1VjBVZUmNFZHJiN09tLWENDT25oOGNrS3c0
elements = soup.find_all('a', {'class': "watchlink"})
要获得如下元素:
<a class="watchlink" href="https://www1.swatchseries.to/freecale.html?r=iexyJrjdCI6InRuT0tnVpFR1VjBVZUmNFZHJiN09tLWENDT25oOGNrS3c0SkkzTDRLSXBUK1VCUXlOd0NJNW1uWWJkWVUrRkluejciLCJpdiI6IjEyNjViZTg2NTU3NWVkN2ZlNDZiNWVjZTA5NjkxNDE2IiwicyI6IjBhZGYxOGNmY2ExMzA5YjEifQ==" onclick="$(this).css('color','#AE3939'); $(this).css('text-decoration','line-through');" rel="nofollow" style="cursor:pointer;" target="_blank" title="mixdrop.co">Watch<span> This</span> Link!</a>
我得到这个错误:
Traceback (most recent call last):
File "C:/Users/CobraCommander/PycharmProjects/999/get_vidtodo_links.py", line 25, in <module>
print(x['title'])
File "C:\Python37\lib\site-packages\bs4\element.py", line 1016, in __getitem__
return self.attrs[key]
KeyError: 'title'
回溯(最近一次呼叫最后一次):
文件“C:/Users/CobraCommander/PycharmProjects/999/get\u vidtodo\u links.py”,第25行,在
打印(x[“标题”])
文件“C:\Python37\lib\site packages\bs4\element.py”,第1016行,在\uuu getitem中__
返回self.attrs[键]
KeyError:“标题”
但是,它确实适用于其他属性,例如“href”
如何从我的元素中获得标题:“mixtrop.co”?只要确保使用
title
访问正确的锚定
标记,因为我相信有些标记具有相同的类名,但没有标题
从bs4导入美化组
html=“”“
soup=BeautifulSoup(html,'html.parser')
目标=[soup.findAll中项目的项目['title'](
“a”,{'class':'watchlink','title':True})]
打印(目标)
输出:
['mixdrop.co']
['mixdrop.co']