BeautifulSoup:从锚点标签中提取href

我一直在阅读其他人的方法,但我无法连接这里的点,我需要从锚点标签中提取href:“/agente/listing/details/5828063”。(下面代码的第二行)


源页面代码段如下所示:


<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&amp;h=160"

        data-original="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&amp;h=160">

      <!-- /ko -->

      <!-- ko if: ListingPictureUrl == '' || ListingPictureUrl == null -->

      <!-- /ko -->

    </span>

    <!-- ko if: MLS -->

    <!-- /ko -->

  </div>

</div>


长风秋雁
浏览 144回答 2
2回答

阿波罗的战车

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

慕娘9325324

from bs4 import BeautifulSouphtml = """<div class="col-md-3" style="margin: 12px auto;">&nbsp; <a title="Abrir imóvel numa nova tab" data-bind="attr:{ href: '/agente/listing/details/' + ID }" target="_blank"&nbsp; &nbsp; href="/agente/listing/details/5828063">&nbsp; &nbsp; <span class="glyphicon glyphicon-new-window"></span>&nbsp; </a>&nbsp; <div class="discount-container loaded">&nbsp; &nbsp; <div data-bind="if: CampaingDescription"></div>&nbsp; &nbsp; <span data-bind="click: $parent.ShowListingDetails, attr:{ id: 'bkmimg' + ID }" style="cursor:pointer"&nbsp; &nbsp; &nbsp; id="bkmimg5828063">&nbsp; &nbsp; &nbsp; <!-- ko if: ListingPictureUrl != '' && ListingPictureUrl != null -->&nbsp; &nbsp; &nbsp; <img class="picture" data-bind="attr:{ src:ListingPictureUrl, 'data-original': ListingPictureUrl}"&nbsp; &nbsp; &nbsp; &nbsp; onerror="this.src='/agente/images/default-listing.png'"&nbsp; &nbsp; &nbsp; &nbsp; src="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&amp;h=160"&nbsp; &nbsp; &nbsp; &nbsp; data-original="https://remaxpt-media.azurewebsites.net/images/listings/12204/122041118-203/L_07e3e17f83064ad2a228f234bf57b32a.jpg?w=160&amp;h=160">&nbsp; &nbsp; &nbsp; <!-- /ko -->&nbsp; &nbsp; &nbsp; <!-- ko if: ListingPictureUrl == '' || ListingPictureUrl == null -->&nbsp; &nbsp; &nbsp; <!-- /ko -->&nbsp; &nbsp; </span>&nbsp; &nbsp; <!-- ko if: MLS -->&nbsp; &nbsp; <!-- /ko -->&nbsp; </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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python