Advertisement
GalinaKG

Real api examples

Jun 15th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. # real api example 1 - Bitcoin price:
  2. import json
  3. import requests
  4.  
  5. # api key/request url
  6. key = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"
  7.  
  8. # requests data from url
  9. data = requests.get(key)
  10. data = data.json()
  11. print(f"{data['symbol']} price is {data['price']}")
  12.  
  13.  
  14. # real api example 2 - BBC news:
  15. import requests
  16.  
  17.  
  18. def BBB_news_func():
  19.  
  20.     # source, sortBy and apiKey
  21.     query_params = {
  22.         "source": "bbc-news",
  23.         "sortBy": "top",
  24.         "apiKey": "4dbc17e007ab436fb66416009dfb59a8"
  25.     }
  26.     main_url = "https://newsapi.org/v1/articles"
  27.  
  28.     # fetching data in json format
  29.     result = requests.get(main_url, params=query_params)
  30.     bbc_page_format = result.json()
  31.  
  32.     # getting all articles in a string article
  33.     articles = bbc_page_format["articles"]
  34.  
  35.     # contain all trending news
  36.     results = []
  37.  
  38.     for current_story in articles:
  39.         results.append(current_story["title"])
  40.  
  41.     for i in range(len(results)):
  42.         # printing all trending news
  43.         print(i + 1, results[i])
  44.  
  45.  
  46. # Driver Code
  47. if __name__ == '__main__':
  48.     # function call
  49.     BBB_news_func()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement