[英]Extract text from within parenthesis into pandas dataframe
我對使用python
抓取數據有點python
,我試圖將數據從這個頁面拉到一個pandas
dataframe
, column
標題如該頁面所示。
現在我有以下代碼,它允許我從頁面中提取數據,但我無法弄清楚以我需要的格式獲取數據的后續步驟。
import requests
url = 'https://mspotrace.org.my/Opmc_list/getCBbyfilters'
r = requests.get(url)
page = requests.get(url).text
您可以使用pandas
api 直接從 url 讀取表格。
>>> import pandas as pd
>>> url = 'https://mspotrace.org.my/Opmc_list'
>>> df = pd.read_html(url)
>>> df[0]
pandas
api, read_html
讀取所有表並返回數據幀列表在您的情況下,該 url 中只有一個表。 所以所需的數據幀位於索引0
編輯
嘗試這個
>>> data = json.loads(page)
>>> df = pd.DataFrame(data)
>>> df
draw recordsTotal recordsFiltered data
0 0 2654 2654 [OPMC31001, Apave Malaysia Sdn Bhd, Part 3, Ka...
1 0 2654 2654 [OPMC31002, Apave Malaysia Sdn Bhd, Part 3, Ko...
2 0 2654 2654 [OPMC31003, Apave Malaysia Sdn Bhd, Part 3, Ko...
3 0 2654 2654 [OPMC31004, Apave Malaysia Sdn Bhd, Part 3, Ko...
4 0 2654 2654 [OPMC31005, Apave Malaysia Sdn Bhd, Part 3, Ko...
... ... ... ... ...
2649 0 2654 2654 [SCCS2333, Trans Certification Interntional Sd...
2650 0 2654 2654 [SCCS2351, TUV Rheinland Malaysia Sdn. Bhd., S...
2651 0 2654 2654 [SCCS1636, DQS Certification (M) Sdn Bhd, SCCS...
2652 0 2654 2654 [SCCS2906, TUV NORD (MALAYSIA) SDN BHD, SCCS, ...
2653 0 2654 2654 [SCCS02085, BSI Services Malaysia Sdn Bhd, SCC...
[2654 rows x 4 columns]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.