前提・実現したいこと
下記htmlデータの<div>名前1(受信者)<br/>名前2(受信者)<br />名前3(受信者)<br />内容</div>を名前(受信者)と内容を分けてエクセルに書き出しを行いたい。
エクセルの書き出し:エクセルのA列に名前(発信者)、B列に日付と時間、C列に名前1(受信者)、名前2(受信者)、名前3(受信者)、D列に内容としたい。
pythonではエラーメッセージは出ませんが、エクセルはA列とB列だけ書き出しでき、C列、D列は書き出しされない状況です。ネットなどで情報も確認してみましたが、見つからずアドバイスコメントをいただけないでしょうか。
どうぞよろしくお願い致します。
【htmlデータ】
<div class="d-chat_timeline-post"> <p class="d-chat_timeline-name"> 名 前(発信者) </p> <ul class="d-chat_timeline-info"> <li>日付と時間 </li> </ul> <div>名前1(受信者)<br /> 名前2(受信者)<br /> 名前3(受信者)<br /> 内容</div>発生している問題・エラーメッセージ
pythonではエラーメッセージは出ませんが、エクセルはA列とB列だけ書き出しできる。C列、D列は書き出しされない。
該当のソースコード
python
1import os, tkinter, tkinter.filedialog, tkinter.messagebox 2root = tkinter.Tk() 3root.withdraw() 4fTyp = [("","*.html")] 5file = tkinter.filedialog.askopenfilename(filetypes = fTyp) 6fd=open(file,"r",encoding="utf-8") 7 8res=fd.read() 9from bs4 import BeautifulSoup 10 11mojie=[] 12cur_pos=0 13while True : 14 target_tag = '<p class="d-chat_timeline-name">' 15 closing_tag = '</p>' 16 start_pos = res[cur_pos:].find(target_tag) 17 end_pos = res[cur_pos:].find(closing_tag) 18 if (start_pos == -1) or (end_pos == -1): 19 break 20 mojie.append(res[cur_pos + start_pos + len(target_tag) : cur_pos + end_pos]) 21 22 cur_pos = cur_pos + end_pos + len(closing_tag) 23 24 25import xlsxwriter 26wb = xlsxwriter.Workbook('C:/Users/XXXXXXX/Desktop/out.xlsx') 27ws1 = wb.add_worksheet('sheet') 28 29i=0 30for a in filter(lambda x:x != "",mojie): 31 i=i+1 32 ws1.write(i, 0, a) 33 34mojie=[] 35cur_pos=0 36while True : 37 target_tag = '<ul class="d-chat_timeline-info">' 38 closing_tag = '</li>' 39 start_pos = res[cur_pos:].find(target_tag) 40 end_pos = res[cur_pos:].find(closing_tag) 41 if (start_pos == -1) or (end_pos == -1): 42 break 43 mojie.append(res[cur_pos + start_pos + len(target_tag) : cur_pos + end_pos]) 44 45 cur_pos = cur_pos + end_pos + len(closing_tag) 46 47i=0 48for a in filter(lambda x:x != "",mojie): 49 i=i+1 50 ws1.write(i, 1, a) 51 52mojie=[] 53cur_pos=0 54while True : 55 target_tag = '<div>' 56 closing_tag = ‘<br/>' 57 start_pos = res[cur_pos:].find(target_tag) 58 end_pos = res[cur_pos:].find(closing_tag) 59 if (start_pos == -1) or (end_pos == -1): 60 break 61 mojie.append(res[cur_pos + start_pos + len(target_tag) : cur_pos + end_pos]) 62 63 cur_pos = cur_pos + end_pos + len(closing_tag) 64 65i=0 66for a in filter(lambda x:x != "",mojie): 67 i=i+1 68 ws1.write(i, 2, a) 69 70mojie=[] 71cur_pos=0 72while True : 73 target_tag = ‘<br/>' 74 closing_tag = ‘</div>' 75 start_pos = res[cur_pos:].find(target_tag) 76 end_pos = res[cur_pos:].find(closing_tag) 77 if (start_pos == -1) or (end_pos == -1): 78 break 79 mojie.append(res[cur_pos + start_pos + len(target_tag) : cur_pos + end_pos]) 80 81 cur_pos = cur_pos + end_pos + len(closing_tag) 82 83i=0 84for a in filter(lambda x:x != "",mojie): 85 i=i+1 86 ws1.write(i, 3, a) 87 88 89wb.close() 90
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/08 02:06