我正在使用 BS4 进行网页抓取,并具有以下 html
:
<a style="display:inline; position:relative;" href="
/aems/file/filegetrevision.do?fileEntityId=8120070&cs=LU31NT9us5P9Pvkb1BrtdwaCrEraskiCJcY6E2ucP5s.xyz">
Screenshot.docx </a>
现在如何使用 BS4 获取 href
的值,无法获取。你能帮忙吗?
谢谢,
最佳答案
这还不够吗?
for a in soup.find_all('a', href=True):
print a['href']
如果你需要你可以在 find_all
中使用 attrs:
soup.find_all("div", {"style": "display:inline; position:relative;"})
去除空格并使链接成为绝对链接:
import urlparse
urlparse.urljoin(url, a['href'].strip())
关于python - 无法使用 BS4 从 <a> 标签中提取 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14196538/