我有一系列HTML文件存储在本地文件夹(“目标文件夹”)中。这些HTML文件都包含许多表。我想要做的是找到我感兴趣的表,感谢关键字,完整地抓取这些表,将它们粘贴到文本文件并将此文件保存到同一本地文件夹(“目标文件夹”)。
这就是我现在所拥有的:
soup = BeautifulSoup(filename,"lxml")
data = []
for keyword in keywords.split(','):
u=1
txtfile = destinationFolder + ticker +'_'+ companyname[:10]+ '_'+item[1]+'_'+item[3]+'_'+keyword+u+'.txt'
mots = soup.find_all(string=re.compile(keyword))
for mot in mots:
for row in mot.find("table").find_all("tr"):
data = cell.get_text(strip=True) for cell in row.find_all("td")
data = data.get_string()
with open(txtfile,'wb') as t:
t.write(data)
txtfile.close()
u=u+1
except:
pass
filename.close()
不确定后台发生了什么,但我最终没有得到我的txt文件,就像我应该的那样。这个过程不会失败。它运行到最后但是完成后我的本地文件夹中找不到txt文件。我确定我正在寻找正确的文件夹。在我的代码中的其他地方使用相同的路径并且工作正常。