2016-12-22 6 views
0

通常のtd/trフォーマットではないhtmlからテーブルをダウンロードしようとしています。画像が含まれており、結果をExcelに保存しています。PythonテーブルをダウンロードしてExcelに保存

htmlコードは次のようになります。[場所50箇所のうち45、50ポイントのうち45サービス]:

<div class="dynamicBottom"> 
<div class="dynamicLeft"> 
<div class="content_block details_block scroll_tabs" data-tab="TABS_DETAILS"> 
<div class="header_with_improve wrap"> 
<a href="/UpdateListing.html" onclick="ta.setEvtCookie('UpdateListing', 'entry-detail-moreinfo', null, 0, '/UpdateListingRedesign')"><div class="improve_listing_btn ui_button primary small">improve this entry</div></a> 
<h3 class="tabs_header">Details</h3> </div> 
<div class="details_tab"> 
<div class="table_section"> 
<div class="row"> 
<div class="ratingSummary wrap"> 
<div class="histogramCommon bubbleHistogram wrap"> 
<div class="colTitle"> 
Rating 
</div> 
<ul class="barChart"> 
<li> 
<div class="ratingRow wrap"> 
<div class="label part "> 
<span class="text">Location</span> 
</div> 
<div class="wrap row part "> 
<span class="rate sprite-rating_s rating_s"> <img class="sprite-rating_s_fill rating_s_fill s45" src="https://static.tacdn.com/img2/x.gif" alt="45 out of fifty points"> 
</span> 
</div> 
</div> 
<div class="ratingRow wrap"> 
<div class="label part "> 
<span class="text">Service</span> 
</div> 
<div class="wrap row part "> 
<span class="rate sprite-rating_s rating_s"> <img class="sprite-rating_s_fill rating_s_fill s45" src="https://static.tacdn.com/img2/x.gif" alt="45 out of fifty points"> 
</span> 
</div> 
</div> 
</li> 

私はテーブルを取得したいと思います。結果をExcelファイルに保存します。 Excelファイルの列見出しには、「場所」と「45」または「50点のうち45」の下にセルが表示されます。次の列ヘッダーには「Service」と表示され、下の行のセルには「45」または「50点のうち45」と表示されます。場所の名前と評価を保存することができますが、サービス評価のセルは空のままです。

私のPythonのコードは次のようになります。

workbook = xlsxwriter.Workbook('File.xlsx') 
worksheet = workbook.add_worksheet() 
row=1 
col=0 
for url in urls: 
    r=requests.get(url) 
    soup=BeautifulSoup(r.content, "lxml") 

    worksheet.write('A1', 'name') 
    worksheet.write('B1', 'location') 
    worksheet.write('C1', 'service') 

    row += 1 
    name= soup.find_all("div", {"class": "LocationName"}) 
    for item in name: 
     worksheet.write_string(row, col, item.text) 
    for div in soup.find_all('div', class_="ratingRow wrap"): 
     text = div.text.strip() 
     alt = div.find('img').get('alt') 
     print(text, alt) 
     worksheet.write_string(row, col+1, alt) 

印刷機能は、場所やサービスのが、Excelシートの場所のためだけの評価で

Location 45 out of fifty points 
Service 45 out of fifty points 

コンソール版画すべての結果を提供しますサービスレーティングのセルが空のままである間に表示されます。列挙関数を試しましたが、Excelの1行にある各セルの場所の評価の文字は1つだけですが、サービス評価の結果も表示されません。

0 4 
1 5 
2 
3 o 
4 u 
5 t 
6 
7 o 
8 f 
9 
10 f 
11 i 
12 f 
13 t 
14 y 
15 
16 p 
17 o 
18 i 
19 n 
19 t 
20 s 

私はExcelの「サービス」の下のセルに「45 50ポイントのうち、」印刷テキストの2行目を保存するためのPythonを伝えることができます任意の方法はありますか?私は徹底的に調査しましたが、解決策はまだ見つかりませんでした。ご助力ありがとうございます!

答えて

0

私は2つの別々のループを持っている理由を理解できませんし、クラスLocationNameがHTML内のどこに表示されるのか分かりません。私はあなたがそれについて何の結果も得られないことを期待しているので、最初のループに何も書いていないと思います。 2番目のループでtextを(行、列)に書き込むように見えます。

最初のループでは、ページ内で1回だけ発生するHTML内の他のデータからの名前を使用します。

(行、列+ 1)のセルのありそうな上書きを避けるために、私の提案:

workbook = xlsxwriter.Workbook('File.xlsx') 
worksheet = workbook.add_worksheet() 
row=1 
for url in urls: 
    col=0 
    r=requests.get(url) 
    soup=BeautifulSoup(r.content, "lxml") 

    worksheet.write('A1', 'name') 
    worksheet.write('B1', 'location') 
    worksheet.write('C1', 'service') 

    row += 1 
    name= soup.find_all("div", {"class": "LocationName"}) 
    for item in name: 
     worksheet.write_string(row, col, item.text) 
    for div in soup.find_all('div', class_="ratingRow wrap"): 
     col+=1 
     text = div.text.strip() 
     alt = div.find('img').get('alt') 
     print(text, alt) 
     worksheet.write_string(row, col, alt) 
+0

申し訳ありませんが、私はHTMLのcode'

Hotel Beispiel
' –

+0

にこの行を忘れて申し訳ありませんが、私はこの行を忘れてしまいましたhtmlコード「

Hotel Beispiel
」とその名前が実際にExcelファイルに出力されます。サービスレーティングが空のセルだけが残されます。私はそれに行くことができる方法はありますか?たぶん、データフレームを使用して? –

+0

ok(私はコメントとして残しておくべきだと思います)ので、各ホテルのロケーションとサービスがあり、あなたはExcelシートにそれらの代替テキストを取得することに興味があります。あなたが書きたい文字列を印刷できるので、xlsxwriterとかなりよく隔離された問題があるように思えます。私は簡単に複数のセルに文字列を書き込んで、これを自分で複製しようとします。あなたがやっているもう一つのことは、2番目のループの上書き(行、col + 1)です、私はデバッグ方法としてそこにあなたの 'colの値を反復しようとします。 – AlanSE

関連する問題