NotImplementedError:尝试在目录中的所有.html文件上循环时不支持非相对模式

2024-05-29 02:43:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图循环遍历目录中的所有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

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


Tags: topathfromimport目录parserhtml错误
1条回答
网友
1楼 · 发布于 2024-05-29 02:43:52

使用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

相关问题 更多 >

    热门问题