2 回答
TA贡献1862条经验 获得超6个赞
您可以使用以下命令获取标签的属性aget()
法典:
from bs4 import BeautifulSoup
html = """<div class="col-md-3" style="margin: 12px auto;">
<a title="Abrir imóvel numa nova tab" data-bind="attr:{ href: '/agente/listing/details/' + ID }" target="_blank"
href="/agente/listing/details/5828063">
<span class="glyphicon glyphicon-new-window"></span>
</a>
<div class="discount-container loaded">
<div data-bind="if: CampaingDescription"></div>
<span data-bind="click: $parent.ShowListingDetails, attr:{ id: 'bkmimg' + ID }" style="cursor:pointer"
id="bkmimg5828063">
<!-- ko if: ListingPictureUrl != '' && ListingPictureUrl != null -->
<img class="picture" data-bind="attr:{ src:ListingPictureUrl, 'data-original': ListingPictureUrl}"
onerror="this.src='/agente/images/default-listing.png'"
src="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&h=160"
data-original="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&h=160">
<!-- /ko -->
<!-- ko if: ListingPictureUrl == '' || ListingPictureUrl == null -->
<!-- /ko -->
</span>
<!-- ko if: MLS -->
<!-- /ko -->
</div>
</div>"""
soup = BeautifulSoup(html, "html.parser")
alink = soup.find('a')
print(alink.get('href'))
结果:
/agente/listing/details/5828063
TA贡献1783条经验 获得超4个赞
from bs4 import BeautifulSoup
html = """<div class="col-md-3" style="margin: 12px auto;">
<a title="Abrir imóvel numa nova tab" data-bind="attr:{ href: '/agente/listing/details/' + ID }" target="_blank"
href="/agente/listing/details/5828063">
<span class="glyphicon glyphicon-new-window"></span>
</a>
<div class="discount-container loaded">
<div data-bind="if: CampaingDescription"></div>
<span data-bind="click: $parent.ShowListingDetails, attr:{ id: 'bkmimg' + ID }" style="cursor:pointer"
id="bkmimg5828063">
<!-- ko if: ListingPictureUrl != '' && ListingPictureUrl != null -->
<img class="picture" data-bind="attr:{ src:ListingPictureUrl, 'data-original': ListingPictureUrl}"
onerror="this.src='/agente/images/default-listing.png'"
src="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&h=160"
data-original="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&h=160">
<!-- /ko -->
<!-- ko if: ListingPictureUrl == '' || ListingPictureUrl == null -->
<!-- /ko -->
</span>
<!-- ko if: MLS -->
<!-- /ko -->
</div>
</div>"""
soup = BeautifulSoup(html, 'html.parser')
target = soup.find("a", {'title': 'Abrir imóvel numa nova tab'}).get("href")
print(target)
输出:
/agente/listing/details/5828063
添加回答
举报