Arthur Law
Arthur Law

Reputation: 131

How to extract Json data into pandas df?

Trying to extract the news title from this page. The data shown on screen looks like json format. Each nodeid is a news entry. However, the following codes do not work:

import pandas as pd
urlco = "https://www.mpfinance.com/fin/getlisting.php?block=daily&startissue=20200118&fixiss=1"

print(urlco)
dfA = pd.read_json(urlco)
print(dfA)

Upvotes: 1

Views: 81

Answers (2)

yganalyst
yganalyst

Reputation: 494

you can try this:

import json
import pandas as pd
from urllib.request import urlopen
urlco = "https://www.mpfinance.com/fin/getlisting.php?block=daily&startissue=20200118&fixiss=1"
response = urlopen(urlco)
response = response.read().decode('utf-8-sig')
result = json.loads(response)
df = pd.DataFrame(result['listing'])
print(df.head())
          nodeid  docissue docsection sectionname docparent parentname  \
0  1579286154686  20200118     S00002          財經    S00001       報章內容   
1  1579286155903  20200118     S00002          財經    S00001       報章內容   
2  1579286156356  20200118     S00002          財經    S00001       報章內容   
3  1579286156402  20200118     S00002          財經    S00001       報章內容   
4  1579286156808  20200118     S00002          財經    S00001       報章內容   

  ordertext                               heading1 heading2  \
0      eaa1  中國出生率70年最低 失人口紅利 勞動人口連降8年 人均收入增速低過GDP            
1      eaa2                    去年GDP增6.1% 第4季保六靠投資            
2      eaa3                   人行全周淨投放9000億 創單周歷史新高            
3      eab1                    去年港銀貸款增6.7% 料今年息差受壓            
4      eab2                         招行業績快報 去年多賺15%            

                                     alt  ...     pubdate   pubdate2  \
0  中國出生率70年最低 失人口紅利 勞動人口連降8年 人均收入增速低過GDP  ...  2020年1月18日  2020-1-18   
1                    去年GDP增6.1% 第4季保六靠投資  ...  2020年1月18日  2020-1-18   
2                   人行全周淨投放9000億 創單周歷史新高  ...  2020年1月18日  2020-1-18   
3                    去年港銀貸款增6.7% 料今年息差受壓  ...  2020年1月18日  2020-1-18   
4                         招行業績快報 去年多賺15%  ...  2020年1月18日  2020-1-18   

                                                link  tag author imprate  \
0  ../fin/daily2.php?node=1579286154686&issue=202...  經濟;                  
1  ../fin/daily2.php?node=1579286155903&issue=202...  經濟;                  
2  ../fin/daily2.php?node=1579286156356&issue=202...  經濟;                  
3  ../fin/daily2.php?node=1579286156402&issue=202...  經濟;                  
4  ../fin/daily2.php?node=1579286156808&issue=202...  經濟;                  

  realnativeads                                             bimage video  \
0         False  https://fs.mingpao.com/fin/20200118/s00002/0fe...         
1         False  https://fs.mingpao.com/fin/20200118/s00002/0fe...         
2         False                                                            
3         False  https://fs.mingpao.com/fin/20200118/s00002/0fe...         
4         False                                                            

   videoplayer  
0               
1               
2               
3               
4               

[5 rows x 23 columns]

Upvotes: 1

Yaakov Bressler
Yaakov Bressler

Reputation: 12168

The issue here is that the json you're trying to load isn't in the correct format for an automatic pandas load. Some light manipulation yields the desired results.

The following code should work:

import pandas as pd
import json, urllib

url = "https://www.mpfinance.com/fin/getlisting.php?block=daily&startissue=20200118&fixiss=1"

response = urllib.request.urlopen(url)
data = json.loads(response.read())

df = pd.DataFrame.from_records(data.get('listing'))

# print(df.head(2))
    nodeid  docissue    docsection  sectionname     docparent   parentname  ordertext   heading1    heading2    alt     summary     time    time2   pubdate     pubdate2    link    tag     author  imprate     realnativeads   bimage  video   videoplayer
0   1579286154686   20200118    S00002  財經  S00001  報章內容    eaa1    中國出生率70年最低 失人口紅利 勞動人口連降8年 人均收入增速低過GDP       中國出生率70年最低 失人口紅利 勞動人口連降8年 人均收入增速低過GDP   【明報專訊】國家統計局昨日公布2019年主要經濟數據,全年GDP增速6.1%,守住6%至6.5%的官方目標區間,GDP總...     2020-01-18 04:30:00         2020年1月18日  2020-1-18   ../fin/daily2.php?node=1579286154686&issue=20200118     經濟;             False   https://fs.mingpao.com/fin/20200118/s00002/0fe56067030fcabe89cf843bc312b479.jpg         
1   1579286155903   20200118    S00002  財經  S00001  報章內容    eaa2    去年GDP增6.1% 第4季保六靠投資         去年GDP增6.1% 第4季保六靠投資     【明報專訊】國家統計局昨日公布2019年中國經濟數據,去年第四季GDP增速6%,與第三季持平,全年GDP則增長6.1%,...     2020-01-18 04:30:00         2020年1月18日  2020-1-18   ../fin/daily2.php?node=1579286155903&issue=20200118     經濟;             False   https://fs.mingpao.com/fin/20200118/s00002/0fe58f80b880233386a37a7fb11ea929.jpg     ```

Upvotes: 3

Related Questions