在html中获取href

2024-10-01 07:15:17 发布

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

我使用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中包含的内容

谢谢


Tags: httpsimageurlstylehtmlblockpostclass
1条回答
网友
1楼 · 发布于 2024-10-01 07:15:17

只需使用['attribute_name']这将按属性名称获取属性

soup.find_all('a', attrs={'class':'block'})[0]['href']
>>> '/post/BpkL7ColOVj'

您还可以使用css selector,我认为这更简单:

soup.select('a.block')[0]['href'] # same thing.

相关问题 更多 >