首页 > 解决方案 > 从字符串中剪切特定部分,删除 HTML 标签。[网页抓取]

问题描述

我得到如下结果:

<a class="ellipsis" href="https://www.link.com" title="Name of the hyperlink ">Name of the hyperlink </a>

我只想提取一个变量 ex 的链接。链接,以及另一个前任的名称。姓名。到目前为止,这是我的代码。

def supa(linko):
    r = get(linko, headers=ua)
    return BeautifulSoup(r.content, 'html.parser')


soup = supa(base_url + search)
the_icons = soup.find_all('div', class_='caption')

for icon in the_icons:
    name = icon.find('a', class_='ellipsis')

    print(name)

标签: pythonweb-scrapingbeautifulsoup

解决方案


您可以在 find 末尾添加['href'] :

for icon in the_icons:
    name = icon.find('a', class_='ellipsis')['href']

推荐阅读