import re
import csv
with open('source.html', 'r', encoding='UTF-8') as f:
source = f.read()
#print(source)
resultList =[]
all_data = re.findall('l_post l_post_bright j_l_post clearfix "(.*?)p_props_tail props_appraise_wrap',source,re.S)
#print(all_data)
for item in all_data:
result = {}
result['name'] = re.findall('username="(.*?)" class',item,re.S)[0]
result['time'] = re.findall('class="tail-info">(2016.*?)</span',item,re.S)[0]
result['content'] = re.findall('j_d_post_content ">(.*?)<',item,re.S)[0]
resultList.append(result)
#print(content)
with open('baidutieba.csv','w',encoding='UTF-8') as f:
writer = csv.DictWriter(f, fieldnames=['name', 'content', 'time'])
writer.writeheader()
writer.writerows(resultList)
网友评论