首页 > 解决方案 > 如何从这个网页抓取的 HTML 中提取某些元素

问题描述

这是我抓取的 HTML。如何提取名为“我要提取的代码”的文本,然后将其保存为字符串“作者”?提前致谢!

<a class="lead-author-profile-link" href="https://papers.ssrn.com/sol3/cf_dev/AbsByAuth.cfm?per_id=2994282" target="_blank" title="View other papers by this author"><span>Code I want to Extract</span><i aria-hidden="true" class="icon icon-gizmo-navigate-right"></i></a>

标签: pythonweb-scrapingbeautifulsoup

解决方案


你可以试试看:

html_doc="""
<a class="lead-author-profile-link" href="https://papers.ssrn.com/sol3/cf_dev/AbsByAuth.cfm?per_id=2994282" target="_blank" title="View other papers by this author"><span>Code I want to Extract</span><i aria-hidden="true" class="icon icon-gizmo-navigate-right"></i></a>
"""
soup = BeautifulSoup(html_doc, 'lxml')
author = soup.find('a').text
print(author)

输出将是:

Code I want to Extract

推荐阅读