首页 > 解决方案 > 需要去除过滤后的解析 HTML 链接中的标签

问题描述

从众多链接中选择一个所需链接后,需要进一步将此链接传递给aDataFrame以提取数字。

我的代码如下:

response=requests.get(url)
soup=BeautifulSoup(response.content,"html.parser")
tags=soup.select_one(a[href*=mar]') *** Out of Jan, Feb and Mar links, the line filters for a link that has Mar in it.

现在我得到“'a href="http://someurl.xlsx">March (12kb, Excel)”'

我只需要“ http://someurl.xlsx ”来传递给aDataFrame以读取Excel 页面的内容。

标签: pythonhtmlweb-scrapingbeautifulsoup

解决方案


尝试以下

from bs4 import BeautifulSoup

html = '<a href="http://someurl.xlsx">March (12kb, Excel)</a>'

soup = BeautifulSoup(html, features="lxml")

tags = soup.select('a')[0].get('href')
print(tags)

http://someurl.xlsx

推荐阅读