Python 正則表達(dá)式查找

import re

import csv

# data = open('tieba01.txt', 'r', encoding='UTF-8').readlines()

# print(len(data))

with open('tieba01.txt', 'r', encoding='UTF-8') as f:

? ? source= f.read()


#search函數(shù)只能從左至右找出第一個(gè)最大的匹配,findall函數(shù)可以找出所有的匹配,并返回一個(gè)list列表

# findall(pattern, string, flags=0)? re.S忽略換行

result_list= []

username_list= re.findall('username="(.*?)"',source,re.S)#

content_list= re.findall('d_post_content j_d_post_content " style="display:;">(.*?)</div>', source, re.S)

reply_time_list=re.findall('class="tail-info">(2020.*?)<', source, re.S)

def clear_content(src):

? ? newstr= re.sub(r'(<img class.*?>)', '', src) #sub是substitute的所寫(xiě),表示替換;re.sub(pattern, repl, string, count=0, flags=0)

? ? return newstr

print(username_list[0],content_list[12])

for iin range(len(username_list)):

? ? clearstr= clear_content(content_list[i])

? ? result= {'username':username_list[i],

? ? ? ? ? ? ? 'content':clearstr,

? ? ? ? ? ? ? 'time': reply_time_list[i]}

? ? result_list.append(result)

with open('tieba01.csv','w',newline='',encoding='utf-8-sig') as f:

? ? csv_write= csv.writer(f)

? ? writer= csv.DictWriter(f,fieldnames=['username','content','time'])

? ? writer.writeheader()

? ? writer.writerows(result_list)

# with open('E:/dst.csv', 'wb') as dstfile:? # 寫(xiě)入方式選擇wb,否則有空行

#? ? writer = csv.DictWriter(dstfile, fieldnames=['username','content','time'])

#? ? writer.writeheader()? # 寫(xiě)入表頭

#? ? writer.writerows(data)? # 批量寫(xiě)入

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容