P.Jhon
P.Jhon

Reputation: 15

How to extract table using request /pandas using python

I tried the code for exctacting product name, year, values fro table but some where i getting issues . my code:

import requests
import pandas as pd
import pymysql



try:
    df = []
    dates1 = []

    try:

        url = 'http://cpmaindia.com/fiscal_custom_duty.php'
        html = requests.get(url).content
        tab_list = pd.read_html(html)
        tab = tab_list[0]
        tab.apply(lambda x: x.tolist(), axis=1)
        tab = tab.values.tolist()
        print(tab)

    except Exception as e:
        raise e


except Exception as e:
    raise e

This I tried But not getting desire output . want to parse table only. thanks

Upvotes: 0

Views: 591

Answers (1)

chitown88
chitown88

Reputation: 28565

tab_list[0] produces the following:

print (tab)
                                                   0
0  <!-- function MM_swapImgRestore() { //v3.0  va...
1  Custom Duty  Import Duty on Petrochemicals (%)...
2  <!-- body { \tmargin-left: 0px; \tmargin-top: ...

Did you mean to grab tab_list[8]?

Also, if you're using pandas to read in the table from html, there is no need to use requests:

import pandas as pd    

url = 'http://cpmaindia.com/fiscal_custom_duty.php'

tab_list = pd.read_html(url)

table = tab_list[8]

table.columns = table.iloc[0,:]
table = table.iloc[1:,2:-1]

Output:

print (table)
0  Import Duty on Petrochemicals (%)  ... Import Duty on Petrochemicals (%)
1                   Product / Year -  ...                             16/17
2                            Naphtha  ...                                 5
3                           Ethylene  ...                               2.5
4                          Propylene  ...                               2.5
5                          Butadiene  ...                               2.5
6                            Benzene  ...                               2.5
7                            Toluene  ...                               2.5
8                       Mixed Xylene  ...                               2.5
9                        Para Xylene  ...                                 0
10                      Ortho Xylene  ...                                 0
11                              LDPE  ...                               7.5
12                             LLDPE  ...                               7.5
13                              HDPE  ...                               7.5
14                                PP  ...                               7.5
15                               PVC  ...                               7.5
16                                PS  ...                               7.5
17                               EDC  ...                                 2
18                               VCM  ...                                 2
19                           Styrene  ...                                 2
20                               SBR  ...                                10
21                               PBR  ...                                10
22                               MEG  ...                                 5
23                               DMT  ...                                 5
24                               PTA  ...                                 5
25                               ACN  ...                                 5

[25 rows x 7 columns]

Upvotes: 1

Related Questions