【发布时间】:2020-03-17 18:37:37
【问题描述】:
我在 html 中有以下标签,我想只提取 href 内容,即 Quatermass_2_Vintage_Movie_Poster-61-10782 和 Hard Day's Night
<span class="small">
Ref.No:10782<br/>
<a href="Quatermass_2_Vintage_Movie_Poster-61-10782" title="Click for more details and a larger picture of Quatermass 2">
Click for more details and a larger picture of <b>Quatermass 2</b>
</a>
</span>, <span class="small">
Ref.No:10781<br/>
<a href="Hard_Day__039_s_Night_Vintage_Movie_Poster-61-10781" title="Click for more details and a larger picture of Hard Day's Night">
Click for more details and a larger picture of <b>Hard Day's Night</b>
</a>
</span>
下面的python代码让我只能找到整个标签
html = ['table2.html']
with open("table2.html", "r") as f:
contents = f.read()
soup = BeautifulSoup(contents, "lxml")
for name in soup.find_all("span", {"class": "small"}):
print(name)
但是不能只选择href。我试过了
for name in soup.find_all("span", {"class": "small"}.get(href)):
print(name)
我也尝试将 href 引用放在打印语句中
for name in soup.find_all("span", {"class": "small"}:
print(name.get('href'))
有人可以帮忙吗?
【问题讨论】:
标签: python html beautifulsoup tags href