使用一行将Python输出保存到excel

时间:2016-12-15 15:32:21

标签: python excel python-3.x xlsxwriter

我正在尝试将我的Python控制台输出保存到excel文件中。不幸的是,它只保存第一行,但截断了其余部分。

html = '''<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>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
for div in soup.find_all('div', class_="ratingRow wrap"):
    text = div.text.strip()
    alt = div.find('img').get('alt')
    print(text, alt)

这给出了: 位置50分中的45分 服务45分中的45分

我尝试将此保存为excel,添加最后两行:

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+15, text)
        worksheet.write_string(row, col+16, alt)

但这只能保存“位置; 50分中的45分”。我想使用一行但不同的列将输出中给出的信息保存到excel。有什么方法可以做到吗? 非常感谢你的帮助!

0 个答案:

没有答案