NotImplementedError: Non-relative patterns are unsupported from attempted looping over all .html files in a directory

NotImplementedError: Non-relative patterns are unsupported from attempted looping over all .html files in a directory

我正在尝试遍历目录中的所有 html 文件,但出现此错误:

NotImplementedError: Non-relative patterns are unsupported

我使用的代码是:

from bs4 import BeautifulSoup
import argparse
from pathlib import Path

parser = argparse.ArgumentParser(description = ("Script to scrape data from antismash html output"))

parser.add_argument("-p", "--path", help = "give path/to/directory containing antismash outputs", required = True)

args = parser.parse_args()

for file in Path(args.path).glob("/*.html"):
    def scraper(filename):
        soup = BeautifulSoup(open(filename), 'html.parser')
        soup.findAll('a') > os.path.basename(filename).txt

我以前使用过相同的方法,但没有出现错误,所以我不确定发生了什么。

在使用 PathLib 时,您不需要在 glob 调用中使用 /。正确的代码是:

for file in Path(args.path).glob("*.html"):
    def scraper(filename):
        soup = BeautifulSoup(open(filename), 'html.parser')
        soup.findAll('a') > os.path.basename(filename).txt