【问题标题】:Retrieve tag attribute in Python在 Python 中检索标签属性
【发布时间】:2021-07-01 13:16:34
【问题描述】:

我想从 title 属性中获取值,即“xxx”。如何获取?

<p>blabla <span class="ticker-hover-wrapper">(xyz:<a href="examplelink" title="xxx" target="_blank">text</a>)```

My current code looks like this:
``` name = webpage.find('a', target="_blank").a.text['title']
                    print (name)
                    name = " ".join(name)
                    name_data.append(name)```

Any ideas?

【问题讨论】:

标签: python beautifulsoup scrape


【解决方案1】:

使用 beautifulsoup4 你可以这样使用:

from bs4 import BeautifulSoup

soup = BeautifulSoup("html", "lxml")
element = soup.find("a", {"target": "_blank"})
print("The element points to", element["href"])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多