我正在尝试抓取标签的内容。下面是一个 html 示例:
<p><a href="https://requiredlink.com" download>Download<span class="caret">
这是我正在做的事情:
r = requests.get("https://abc.efg.questions").content
html_obj = html.fromstring(r)
soup = BeautifulSoup(r)
for a in soup.find_all("a", text=re.compile("Download")):
print a['href']
打印语句不返回任何内容。我做的有问题吗?
它失败是因为<a>
里面有一个<span>
标签,因此对象的.string()
方法返回None
,您可以使用列表推导式重写列表以使其工作,如下所示:
>>> for a in [s for s in soup.find_all("a") if s.text == "Download"]:
print(a['href'])
https://requiredlink.com