CoCalc Public FilesDemo1.ipynbOpen with one click!
Author: iAwffe Wai
Views : 45
Description: Demo
Compute Environment: Ubuntu 18.04 (Deprecated)
In [1]:
# reference reading # http://jonathansoma.com/lede/foundations-2017/classes/adv-scraping/advanced-scraping-form-submission/
In [ ]:
import requests from bs4 import BeautifulSoup
In [2]:
url="https://www.amcm.gov.mo/api/oamcm/middle-rates"
In [3]:
# calendar day 2018-10-23 (not sure what format is this) cdate = 1540274400
In [5]:
param = dict( start = 1540274400, end = 1540274400, type = display )
In [9]:
response = requests.post(url, data=param) # status code 200 = OK print(response.status_code)
200
In [10]:
soup = BeautifulSoup(response.text, 'html.parser') soup.prettify()
'{"status":1,"code":200,"content":"[{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"AUD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"5.72570000\\",\\"bid\\":\\"0.70910000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"CAD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"6.17240000\\",\\"bid\\":\\"1.30780000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"CHF\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"8.10920000\\",\\"bid\\":\\"0.99540000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"CNY\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"1.16280000\\",\\"bid\\":\\"694.28000000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"DKK\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"1.24100000\\",\\"bid\\":\\"6.50500000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"EUR\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"9.25870000\\",\\"bid\\":\\"1.14670000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"GBP\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"10.48060000\\",\\"bid\\":\\"1.29810000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"HKD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"1.03000000\\",\\"bid\\":\\"1.02900000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"JPY\\",\\"unit\\":\\"100\\",\\"mopMean\\":\\"7.17930000\\",\\"bid\\":\\"112.44000000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"KRW\\",\\"unit\\":\\"100\\",\\"mopMean\\":\\"0.71000000\\",\\"bid\\":\\"1134.50000000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"LIQ\\",\\"unit\\":\\"0\\",\\"mopMean\\":\\"0.00000000\\",\\"bid\\":\\"0.30000000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"MYR\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"1.93960000\\",\\"bid\\":\\"4.16000000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"NOK\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"0.97360000\\",\\"bid\\":\\"8.29200000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"NZD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"5.29020000\\",\\"bid\\":\\"0.65500000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"SEK\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"0.89450000\\",\\"bid\\":\\"9.02470000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"SGD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"5.85840000\\",\\"bid\\":\\"1.37800000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"TWD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"0.26080000\\",\\"bid\\":\\"30.93900000\\"},{\\"valueDate\\":\\"1540310400\\",\\"currency\\":\\"USD\\",\\"unit\\":\\"1\\",\\"mopMean\\":\\"8.07350000\\",\\"bid\\":\\"7.83820000\\"}]"}\n'
In [13]:
import json body = json.loads(str(soup)) print(body['content'])
[{"valueDate":"1540310400","currency":"AUD","unit":"1","mopMean":"5.72570000","bid":"0.70910000"},{"valueDate":"1540310400","currency":"CAD","unit":"1","mopMean":"6.17240000","bid":"1.30780000"},{"valueDate":"1540310400","currency":"CHF","unit":"1","mopMean":"8.10920000","bid":"0.99540000"},{"valueDate":"1540310400","currency":"CNY","unit":"1","mopMean":"1.16280000","bid":"694.28000000"},{"valueDate":"1540310400","currency":"DKK","unit":"1","mopMean":"1.24100000","bid":"6.50500000"},{"valueDate":"1540310400","currency":"EUR","unit":"1","mopMean":"9.25870000","bid":"1.14670000"},{"valueDate":"1540310400","currency":"GBP","unit":"1","mopMean":"10.48060000","bid":"1.29810000"},{"valueDate":"1540310400","currency":"HKD","unit":"1","mopMean":"1.03000000","bid":"1.02900000"},{"valueDate":"1540310400","currency":"JPY","unit":"100","mopMean":"7.17930000","bid":"112.44000000"},{"valueDate":"1540310400","currency":"KRW","unit":"100","mopMean":"0.71000000","bid":"1134.50000000"},{"valueDate":"1540310400","currency":"LIQ","unit":"0","mopMean":"0.00000000","bid":"0.30000000"},{"valueDate":"1540310400","currency":"MYR","unit":"1","mopMean":"1.93960000","bid":"4.16000000"},{"valueDate":"1540310400","currency":"NOK","unit":"1","mopMean":"0.97360000","bid":"8.29200000"},{"valueDate":"1540310400","currency":"NZD","unit":"1","mopMean":"5.29020000","bid":"0.65500000"},{"valueDate":"1540310400","currency":"SEK","unit":"1","mopMean":"0.89450000","bid":"9.02470000"},{"valueDate":"1540310400","currency":"SGD","unit":"1","mopMean":"5.85840000","bid":"1.37800000"},{"valueDate":"1540310400","currency":"TWD","unit":"1","mopMean":"0.26080000","bid":"30.93900000"},{"valueDate":"1540310400","currency":"USD","unit":"1","mopMean":"8.07350000","bid":"7.83820000"}]
In [18]:
import pandas as pd df = pd.read_json(body['content'])
In [20]:
print(df)
bid currency mopMean unit valueDate 0 0.7091 AUD 5.7257 1 1540310400 1 1.3078 CAD 6.1724 1 1540310400 2 0.9954 CHF 8.1092 1 1540310400 3 694.2800 CNY 1.1628 1 1540310400 4 6.5050 DKK 1.2410 1 1540310400 5 1.1467 EUR 9.2587 1 1540310400 6 1.2981 GBP 10.4806 1 1540310400 7 1.0290 HKD 1.0300 1 1540310400 8 112.4400 JPY 7.1793 100 1540310400 9 1134.5000 KRW 0.7100 100 1540310400 10 0.3000 LIQ 0.0000 0 1540310400 11 4.1600 MYR 1.9396 1 1540310400 12 8.2920 NOK 0.9736 1 1540310400 13 0.6550 NZD 5.2902 1 1540310400 14 9.0247 SEK 0.8945 1 1540310400 15 1.3780 SGD 5.8584 1 1540310400 16 30.9390 TWD 0.2608 1 1540310400 17 7.8382 USD 8.0735 1 1540310400
In [31]:
result = df.loc[df['currency']=="LIQ"]['bid'] print(result.values)
[0.3]
In [2]:
int = 2
In [3]:
int
2
In [5]:
type(str)
type
In [ ]: