【发布时间】:2019-10-10 12:43:52
【问题描述】:
所以,我正在制作一个刮板,它会从网站上刮取表格数据,然后将其上传到天蓝色数据库中。我正在尝试使用 Beautiful Soup 抓取数据。该站点是https://www.pgcb.org.bd/PGCB/?a=pages/hourly_generation_loadshed_display.php 问题是该站点的html 代码很粗糙。
</div>
<!-- main container-->
<div class="grid_18" id="main_container">
<div style="padding-left: 10px; padding-top: 5px;"><img
src="images/hgen&loadshed.jpg"/></div>
<head>
<style>
tr:nth-child(even){
background-color: #ccc;
}
tr:hover
{
background: #f7dcdf;
}
</style>
</head>
<table class="layout display responsive-table"><tr>
<th style="text-align: center;">Date</th>
<th style="text-align: center;">Time</th>
<th style="text-align: center;">Generation</th>
<th style="text-align: center;">Demand</th>
<th style="text-align: center;">Shortage</th>
<th style="text-align: center;">Loadshed</th>
<th style="text-align: center;">Remark</th>
</tr> <tr>
<td style="text-align: center;">10-10-2019</td>
<td style="text-align: center;">09:00:00</td>
<td style="text-align: center;">7600.4</td>
<td style="text-align: center;">7600</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;"></td>
</tr>
<tr>
<td style="text-align: center;">10-10-2019</td>
<td style="text-align: center;">08:00:00</td>
<td style="text-align: center;">7165.2</td>
<td style="text-align: center;">7165</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;"></td>
</tr>
<tr>
到目前为止,我已经尝试了以下内容,并得到了上述结果以及其他一些文本,我可以稍后将其删除。但是,我需要从 日期 时间
<td style="text-align: center;">10-10-2019</td>
<td style="text-align: center;">09:00:00</td>
表格格式,如,
日期 |时间 |
10-10-2019 | 9:00:00|
这是我到目前为止所做的:
#import requests
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq # webclient
#scrapping from
page_url = "https://www.pgcb.org.bd/PGCB/?a=pages/hourly_generation_loadshed_display.php"
uclient = uReq (page_url)
#parsing the html
page_soup = soup (uclient.read(), "html.parser")
uclient.close()
table1 = page_soup.findAll("table",{"class":"layout display responsive-table"})
请告诉我如何改进这一点并获得预期的结果。
【问题讨论】:
标签: python html python-3.x web-scraping beautifulsoup