0
import re
fr=open("test.html",'r')
i,j,tablestart=0,0,0
str=""
p=re.compile("<td.*?>(.*?)<\/td>")
for line in fr:
    if "<table" in line:
        tablestart=1
    elif "</table>" in line and tablestart==1:
        j,tablestart=0,0
    m=p.search(line)
    if m and tablestart==1:
        str+='"' + m.group(1) + '"' + ","
    if "</tr>" in line and tablestart==1:
        print(str)
        str=""

该代码是从 html 表创建 csv 文件。有没有更好或更有效的编码方式?我不是在寻找任何 html 解析器。

4

1 回答 1

1

也许是这样的:

for line in fr:
   if re.search(r'"<td.*?>.+?<\/td>"',line):
      line_table = re.findall(r'\>\.+?\<',line)
      var = line_table
      for var1 in var:
         if var1 != False:
             var2 = re.findall(r'\>\.+?\<',var1)[0]
             output.write(var2+','+'\n')
         else:
             output.write(','+'\n')
于 2013-12-22T15:19:23.320 回答