【发布时间】:2021-09-04 18:47:15
【问题描述】:
我的文件夹中有 13,000 个 html 文件 - 我正在尝试将数据放入单个 csv 文件中。
我相信我已经设法让它大部分工作 - 但是似乎在写入 csv 时遇到问题,无论我尝试什么。
这是我当前的代码:
import re
import csv
from bs4 import BeautifulSoup
path = r'C:/Users/Mx/Testing/Infod'
ext = '.htm'
for filename in os.listdir(path):
if filename.endswith(ext):
fullpath = os.path.join(path, filename)
filename = os.path.splitext(os.path.basename(filename))[0]
soup = BeautifulSoup(open(fullpath, encoding="utf-8"), 'html.parser')
text = soup.get_text()
ref = soup.find("td", text="Reference")
pattern = re.compile(r'GBBTI\S{9}')
IC = soup.find("b", text="Issuing country")
cx = IC.findNext("td").contents
SD = soup.find("b", text="Start date of validity")
SDX = SD.findNext("td").contents
ED = soup.find("b", text="End date of validity")
EDX = ED.findNext("td").content
NC = soup.find("b", text="Nomenclature code")
NCX = NC.findNext("td").contents
CJ = soup.find("b", text="Classification justification")
CJX = CJ.findNext("td").contents
L = soup.find("b", text="Language")
LX = L.findNext("td").contents
POI = soup.find("b", text="Place of issue")
POIX = POI.findNext("td").contents
DOI = soup.find("b", text="Date of issue")
DOIX = DOI.findNext("td").contents
NAA = soup.find("b", text="Name and adress")
NAAX = NAA.findNext("td").contents
DOG = soup.find("b", text="Description of goods")
DOGX = DOG.findNext("td").contents
NK = soup.find("b", text="National keywords")
NKX = NK.findNext("td").contents
with open('names.csv', 'w') as csvfile:
fieldnames = ['Ref', 'country', 'Start date of Validity', 'End date of validity', 'Nomenclature code', 'Classification justification', 'Language', 'Place of issue', 'Date of issue', 'Name and address', 'Description', 'keywords']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow((soup.find('td', text=pattern)),cx, SDX, EDX, NCX, CJX, LX, POIX, DOIX, NAAX, DOGX, NKX) ```
Any advice would be greatly appreciated.
【问题讨论】:
-
您面临什么问题?
-
它不会保存到 csv
-
您能否发布运行此脚本后 CSV 的样子?
-
我想你的 CSV 在运行这个脚本后只有两行。 @MxMorrigan
标签: python html csv beautifulsoup