使用BeautifulSoup废弃没有ID和类的动态生成的HTML表

时间:2019-03-03 08:15:38

标签: python-3.x selenium web-scraping beautifulsoup

我使用Python Selenium生成了一个表,生成的表的源代码如下所示(代码具有

  

        <TABLE cellSpacing=0 cellPadding=0 width=806 border=0>
    <TBODY>
    <TR>          <TD width="603" height="25" colspan="4" align="center" bgcolor="#00596B">            
        <b><font color="#FFFFFF">Day End Summary from Jan 03, 2019 to Jan 01, 2019</font></b>             </TD>       </TR>

    <tr>
      <TD vAlign=top align=middle width="603" style="min-height:980px;">
        <table border="0" cellpadding="" width="603" bgcolor="#808000" cellspacing="1">
          <tr bgcolor="#D6D6D6" height="25">
            <td align="center"><font color="#000" size="2">#</font></td>
          <td align="center"><font color="#000" size="2">DATE</font></td>
          <td align="center"><font color="#000" size="2">TRADING CODE</font></td>
            <td align="center"><font color="#000" size="2">LTP*</font></td>
          <td align="center"><font color="#000" size="2">HIGH</font></td>
          <td align="center"><font color="#000" size="2">LOW</font></td>
          <td align="center"><font color="#000" size="2">OPENP*</font></td>
          <td align="center"><font color="#000" size="2">CLOSEP*</font></td>
          <td align="center"><font color="#000" size="2">YCP</font></td>
          <td align="center"><font color="#000" size="2">TRADE</font></td>
          <td align="center"><font color="#000" size="2">VALUE (mn)</font></td>
          <td align="center"><font color="#000" size="2">VOLUME</font></td>                
          </tr>

          <tr bgcolor="#FFFFFF" height="20">
            <td align="center"> <font face="Arial" style="size:11px;">1</font></td>
          <td align="center"> <font face="Arial" style="size:11px;">2019-01-03</font></td>
          <td align="left"><a href = "displayCompany.php?name=ACI" class='ab1'><font face="Arial" style="size:11px;">ACI</font></a></td>
          <td align="right"><font face="Arial" style="size:11px;">328.5</font></td>
          <td align="right"><font face="Arial" style="size:11px;">329.9</font></td>
          <td align="right"><font face="Arial" style="size:11px;">323</font></td>
          <td align="right"><font face="Arial" style="size:11px;">325.7</font></td>
          <td align="right"><font face="Arial" style="size:11px;">328.4</font></td>
          <td align="right"><font face="Arial" style="size:11px;">325.7</font></td>
          <td align="right"><font face="Arial" style="size:11px;">256</font></td>
          <td align="right"><font face="Arial" style="size:11px;">5.285</font></td>
          <td align="right"><font face="Arial" style="size:11px;">16,158</font></td>
          </tr>

          <tr bgcolor="#EFEFEF" height="20">
            <td align="center"> <font face="Arial" style="size:11px;">2</font></td>
          <td align="center"> <font face="Arial" style="size:11px;">2019-01-02</font></td>
          <td align="left"><a href = "displayCompany.php?name=ACI" class='ab1'><font face="Arial" style="size:11px;">ACI</font></a></td>
          <td align="right"><font face="Arial" style="size:11px;">325.5</font></td>
          <td align="right"><font face="Arial" style="size:11px;">328.8</font></td>
          <td align="right"><font face="Arial" style="size:11px;">324.3</font></td>
          <td align="right"><font face="Arial" style="size:11px;">326</font></td>
          <td align="right"><font face="Arial" style="size:11px;">325.7</font></td>
          <td align="right"><font face="Arial" style="size:11px;">327.6</font></td>
          <td align="right"><font face="Arial" style="size:11px;">289</font></td>
          <td align="right"><font face="Arial" style="size:11px;">7.443</font></td>
          <td align="right"><font face="Arial" style="size:11px;">22,851</font></td>
          </tr>

          <tr bgcolor="#FFFFFF" height="20">
            <td align="center"> <font face="Arial" style="size:11px;">3</font></td>
          <td align="center"> <font face="Arial" style="size:11px;">2019-01-01</font></td>
          <td align="left"><a href = "displayCompany.php?name=ACI" class='ab1'><font face="Arial" style="size:11px;">ACI</font></a></td>
          <td align="right"><font face="Arial" style="size:11px;">329.1</font></td>
          <td align="right"><font face="Arial" style="size:11px;">329.9</font></td>
          <td align="right"><font face="Arial" style="size:11px;">325.6</font></td>
          <td align="right"><font face="Arial" style="size:11px;">329.9</font></td>
          <td align="right"><font face="Arial" style="size:11px;">327.6</font></td>
          <td align="right"><font face="Arial" style="size:11px;">326.2</font></td>
          <td align="right"><font face="Arial" style="size:11px;">153</font></td>
          <td align="right"><font face="Arial" style="size:11px;">3.234</font></td>
          <td align="right"><font face="Arial" style="size:11px;">9,890</font></td>
          </tr>

        </table>

页面链接位于Dhaka Stock Exchange Historical Price。 现在,如何从这里提取完整表?该表似乎没有任何ID和类。我对HTML和Web抓取是完全陌生的,需要为时间表非常紧迫的项目执行此操作。任何帮助将不胜感激。

更新:我可以使用以下代码提取所需的表格: def extractdse(日期,时间,工具):

# load the driver
driver = webdriver.Chrome()

# load DSE data archive site
driver.get('https://www.dsebd.org/day_end_archive.php')

# deal with the pop up warning
try: 
    alert = driver.switch_to.alert
    alert.accept()
except NoAlertPresentException:
    print ("No alert here")

# from date
fdate=datetime.datetime.strptime(fdate, '%Y-%m-%d')
yr=str(fdate.year)
mn='0'+str(fdate.month)
dy='0'+str(fdate.day)

# to date
tday=datetime.datetime.strptime(tday, '%Y-%m-%d')
bdd='0'+str(tday.day)
bdm='0'+str(tday.month)
bdy=str(tday.year)

# beginning day
x=Select(driver.find_element_by_name("DayEndSumDate1_day"))
x.select_by_value(dy)

# beginning month
y=Select(driver.find_element_by_name("DayEndSumDate1_month"))
y.select_by_value(mn)

# beginning year
z=Select(driver.find_element_by_name("DayEndSumDate1_year"))
z.select_by_value(yr)

# end day
xp=Select(driver.find_element_by_name("DayEndSumDate2_day"))
xp.select_by_value(bdd)

# end month
yp=Select(driver.find_element_by_name("DayEndSumDate2_month"))
yp.select_by_value(bdm)

# end year
zp=Select(driver.find_element_by_name("DayEndSumDate2_year"))
zp.select_by_value(bdy)

# instrument
inst=Select(driver.find_element_by_name("Symbol"))
inst.select_by_visible_text(instrument)

# generate the table
driver.find_element_by_name("ViewDayEndArchive").click()

# extract the table
soup = BeautifulSoup(driver.page_source, 'html.parser')
tables = soup.find_all('table')
tt=tables[374]
#ti=pd.read_html(tt)
#tm=display_html(tt, raw=True)
#tx=pd.read_html(tt)
return(tt)

现在可以使用-

提取所需表的HTML代码。
extractdse('2019-01-01', '2019-01-03', 'ACI')

现在,有什么方法可以将HTML代码转换为Pandas Dataframe?

1 个答案:

答案 0 :(得分:1)

您可能应该为此使用熊猫。像这样:

my_table = """
  [the code from your question]
 """

import pandas as pd

pad_table = pd.read_html(my_table)
pad_table

这应该可以帮助您达到目标。