使用 beautiful soup 基于类和 href 标签解析 html 标签

wha*_*atf 4 python beautifulsoup

我正在尝试使用 BeautifulSoup 解析 HTML。

我想要的内容是这样的:

<a class="yil-biz-ttl" id="yil_biz_ttl-2" href="http://some-web-url/" title="some title">Title</a> 
Run Code Online (Sandbox Code Playgroud)

我尝试并得到以下错误:

maxx = soup.findAll("href", {"class: "yil-biz-ttl"})
------------------------------------------------------------
   File "<ipython console>", line 1
     maxx = soup.findAll("href", {"class: "yil-biz-ttl"})
                                             ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我想要的是字符串:http://some-web-url/

inf*_*red 8

soup.findAll('a', {'class': 'yil-biz-ttl'})[0]['href']
Run Code Online (Sandbox Code Playgroud)

要查找所有此类链接:

for link in soup.findAll('a', {'class': 'yil-biz-ttl'}):
    try:
        print link['href']
    except KeyError:
        pass
Run Code Online (Sandbox Code Playgroud)