Python 下载 table 并保存到 Excel

Python download table and save to Excel

我正在尝试从 html 下载一个 table,它不是通常的 td/tr 格式,包含图像并将结果保存到 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>

我想得到table:[位置45分,满分50分,服务45分,满分50分]。并将结果保存到 Excel 文件。 Excel 文件中的 column-header 应注明 "Location" 以及“45”或“50 分中的 45”下方的单元格。下一个 column-header 应该写成 "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)

打印函数给出

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

控制台打印位置和服务的所有结果,但在 Excel sheet 中仅显示位置评级,而服务评级单元格仍为空。我尝试了枚举功能,但是 Excel 中的每一行中的每个单元格中只有一个位置评级字符,但服务评级结果也没有出现。

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

有什么方法可以让 Python 将打印文本中的第二行“50 分中的 45 分”保存到 Excel 中 "Service" 下面的单元格中?我彻底搜索但找不到解决方案。非常感谢您的帮助!

我不明白为什么你有 2 个单独的循环,而且我找不到 class LocationName 在 HTML 中出现的任何地方。因为我希望您不会为此得到任何结果,所以我希望在第一个循环中不会写入任何内容 - 与您报告的内容一致。似乎您应该在第二个循环中将 text 写入 (row, col)。

根据讨论,第一个循环使用了 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)