Python – Scraping Data Json

Example 1 :

import requests

# Ambil data dari API dan langsung diolah JSON tersebut
url = 'http://www.floatrates.com/daily/idr.json'
r = requests.get(url).json()

for data in r.values():
    print(data['code'])
    print(data['name'])
    print(data['date'])
    print(data['inverseRate'])
    print('-'*10)


Example 2 :

import requests

url = 'https://website.com'
parameter = {
    'page': 1,
    'per_page': 1,
}
    
r = requests.get(url, params=parameter).json()
posts = r['posts']

for post in posts:
 print(post['title']) 

Using Headers

import requests

url = 'https://website.com/'

headers = {
        'user-agent': user_agent,
    }
response = requests.get(url, headers=headers)

Leave a Reply

Your email address will not be published. Required fields are marked *