我正在尝试使用findall从许多不同的HTML文件中提取元素,并将它们放入一个新的HTML文件中。到目前为止,我已经
news = ['16-10-2017.html', '17-10-2017.html', '18-10-2017.html', '19-10-2017.html', '21-10,2017.html', '22-10-2017.html']
def extracted():
raw_news = open(news, 'r', encoding = 'UTF-8')我正在创建一个函数,它将能够读取这些文件,提取特定的部分,以便我可以将它们放入一个新的html文件中,但我不确定用于读取文件的代码是否正确。我如何才能从这些文件中提取元素。
发布于 2017-10-24 17:46:51
你需要遍历列表,打开一个文件(python会要求一个'string‘,然后说它得到了一个' list’)。一旦进入循环,就可以对文件进行操作,也许可以保存要查找的文本,并将其放入其他数据结构中。将工作目录更改为包含这些文件的目录,然后:
def extracted(news):
for page in news:
raw_news = open(news[page], 'r', encoding = 'UTF-8')
# Now you have raw_news from one page and you can operate over it
# Once the loop is over, the same code would run on the next html filehttps://stackoverflow.com/questions/46906762
复制相似问题