【问题标题】:Get value of attribute using CSS Selectors with BeutifulSoup使用带有 BeautifulSoup 的 CSS 选择器获取属性值
【发布时间】:2026-01-31 00:10:01
【问题描述】:

我正在使用Python 进行网络抓取并使用BeutifulSoup

我有这样的HTML 标记:

<tr class="deals" data-url="www.example2.com">
<span class="hotel-name">
<a href="www.example2.com"></a>
</span>
</tr>
<tr class="deals" data-url="www.example3.com">
<span class="hotel-name">
<a href="www.example3.com"></a>
</span>
</tr>

我想在所有&lt;tr&gt;s 中获取data-urlhref 值。如果我能得到href 的价值就更好了

这是我的相关代码的小sn-p:

main_url =  "http://localhost/test.htm"
page  = requests.get(main_url).text
soup_expatistan = BeautifulSoup(page)

print (soup_expatistan.select("tr.deals").data-url)
# or  print (soup_expatistan.select("tr.deals").["data-url"])

【问题讨论】:

    标签: python css python-3.x beautifulsoup html-parsing


    【解决方案1】:

    您可以使用tr.deals span.hotel-name a CSS Selector 来获取链接:

    from bs4 import BeautifulSoup
    
    data = """
    <tr class="deals" data-url="www.example.com">
    <span class="hotel-name">
    <a href="wwwexample2.com"></a>
    </span>
    </tr>
    """
    
    soup = BeautifulSoup(data)
    print(soup.select('tr.deals span.hotel-name a')[0]['href'])
    

    打印:

    wwwexample2.com
    

    如果您有多个链接,请遍历它们:

    for link in soup.select('tr.deals span.hotel-name a'):
        print(link['href'])
    

    【讨论】: