我想运行一个 python 脚本来解析 html 文件并收集具有target="_blank"
属性的所有链接的列表。
我已经尝试了以下方法,但它没有从 bs4 得到任何东西。SoupStrainer 在文档中说它将以与 findAll 等相同的方式使用 args,这应该有效吗?我错过了一些愚蠢的错误吗?
import os
import sys
from bs4 import BeautifulSoup, SoupStrainer
from unipath import Path
def main():
ROOT = Path(os.path.realpath(__file__)).ancestor(3)
src = ROOT.child("src")
templatedir = src.child("templates")
for (dirpath, dirs, files) in os.walk(templatedir):
for path in (Path(dirpath, f) for f in files):
if path.endswith(".html"):
for link in BeautifulSoup(path, parse_only=SoupStrainer(target="_blank")):
print link
if __name__ == "__main__":
sys.exit(main())