我正在使用BeautifulSoup从html数据表中获取信息。特别是,我试图在以下行中获取href = ...
<a class="block" href="/post/BpkL7ColOVj" style="background-image: url(https://scontent-ort2-2.cdninstagram.com/vp/09e1b7436c9125092433c041c35c1eaa/5BDB064D/t51.2885-15/e15/s480x480/43913877_2130106893692252_5245480330715053223_n.jpg)">
soup.find_all('a', attrs={'class':'block'})
还有其他方法可以使用BeautifulSoup获取href中包含的内容吗?
谢谢!
答案 0 :(得分:1)
只需使用['attribute_name']
,它将按其名称获取属性。
soup.find_all('a', attrs={'class':'block'})[0]['href']
>>> '/post/BpkL7ColOVj'
您也可以使用css selector
,我认为它更简单:
soup.select('a.block')[0]['href'] # same thing.