为了账号安全,请及时绑定邮箱和手机立即绑定

BeautifulSoup:从锚点标签中提取href

BeautifulSoup:从锚点标签中提取href

长风秋雁 2022-08-16 10:49:05
我一直在阅读其他人的方法,但我无法连接这里的点,我需要从锚点标签中提取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>
查看完整描述

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&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>"""


soup = BeautifulSoup(html, "html.parser")

alink = soup.find('a')

print(alink.get('href'))

结果:


/agente/listing/details/5828063


查看完整回答
反对 回复 2022-08-16
?
慕娘9325324

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&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>"""


soup = BeautifulSoup(html, 'html.parser')


target = soup.find("a", {'title': 'Abrir imóvel numa nova tab'}).get("href")

print(target)

输出:


/agente/listing/details/5828063


查看完整回答
反对 回复 2022-08-16
  • 2 回答
  • 0 关注
  • 142 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信