Python Programming

Lecture 13 Web API

13.1 Web Api

Web Api


import requests

url = "http://t.weather.itboy.net/api/weather/city/101020100"
r = requests.get(url)
print(r.status_code)
response_dict = r.json()  

f = response_dict['data']
ff = f['forecast']
ff_today = ff[0]
ff_1 = ff[1]
ff_2 = ff[2]
 
def show(day):
    for x in day:
        print(x+': '+str(day[x]))
    print()
show(ff_today)
show(ff_1)
show(ff_2)

ChatGpt


from openai import OpenAI

client = OpenAI(
    api_key="Your api key",
)
completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": "Explains the concept\ 
    of recursion in programming."}
  ]
)

print(completion.choices[0].message)

GitHub

https://api.github.com/search/repositories?q=language:python&sort=stars


import requests

url = 'https://api.github.com/search/\
repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)
response_dict = r.json()

for keys in response_dict.keys():
    print(keys)

Status code: 200
total_count
incomplete_results
items
About Incomplete Result

Timeouts and incomplete results

To keep the REST API fast for everyone, we limit how long any individual query can run. For queries that exceed the time limit, the API returns the matches that were already found prior to the timeout, and the response has the incomplete_results property set to true. Reaching a timeout does not necessarily mean that search results are incomplete. More results might have been found, but also might not.

  • Working with the Response Dictionary

print("Total repositories:", response_dict['total_count'])

repo_dicts = response_dict['items']
print("Repositories returned:", len(repo_dicts))

repo_dict = repo_dicts[0]
print("\nKeys:", len(repo_dict))

Total repositories: 13150294
Repositories returned: 30

Keys: 80

print("\nSelected information about first repository:")
print('Name:', repo_dict['name'])
print('Owner:', repo_dict['owner']['login'])
print('Stars:', repo_dict['stargazers_count'])
print('Repository:', repo_dict['html_url'])
print('Created:', repo_dict['created_at'])
print('Updated:', repo_dict['updated_at'])
print('Description:', repo_dict['description'])

Selected information about first repository:
Name: public-apis
Owner: public-apis
Stars: 294708
Repository: https://github.com/public-apis/public-apis
Created: 2016-03-20T23:49:42Z
Updated: 2024-05-27T08:51:28Z
Description: A collective list of free APIs
  • Visualizing Repositories Using Plotly

import requests
import plotly.express as px
 
URL = 'https://api.github.com/search/repositories?q=language:python&sort=star'
r = requests.get(URL)
print("Status code:", r.status_code)
response_dict = r.json()

repo_dicts = response_dict['items']
names, stars = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    stars.append(repo_dict['stargazers_count'])

title = "Most-Starred Python Projects on GitHub"
labels = {'x': 'Repository', 'y': 'Stars'}
fig = px.bar(x = names, y=stars, title = title, labels=labels)
fig.write_html('python_repos.html')
Most-Starred Python Projects

Where to find Web Api? Public APIs, 聚合数据, IMDB-API

TMDB-API

import requests

api_access = 'Your API Key'

page = 1
url = f"https://api.tmdb.org/3/movie/\
top_rated?language=en-US&page={page}"

headers = {
    "accept": "application/json",
    "Authorization": f"Bearer {api_access}"
}
response = requests.get(url, headers=headers)
response_dict = response.json()
# print(response_dict)

movies=response_dict["results"]
print(len(movies))

for key, value in movies[0].items():
    print(f"{key}: {value}")

adult: False
backdrop_path: /zfbjgQE1uSd9wiPTX4VzsLi0rGG.jpg
genre_ids: [18, 80]
id: 278
original_language: en
original_title: The Shawshank Redemption
overview: Imprisoned in the 1940s for the double murder of his wife and her lover, 
upstanding banker Andy Dufresne begins a new life at the Shawshank prison, 
where he puts his accounting skills to work for an amoral warden. 
During his long stretch in prison, Dufresne comes to be admired by the other 
inmates -- including an older prisoner named Red -- 
for his integrity and unquenchable sense of hope.
popularity: 115.576
poster_path: /9cqNxx0GxF0bflZmeSMuL5tnGzr.jpg
release_date: 1994-09-23
title: The Shawshank Redemption
video: False
vote_average: 8.705
vote_count: 26204

Downloading Images


from pathlib import Path

poster = movies[0]['poster_path']
title = movies[0]['title']
img_url = f"https://image.tmdb.org/t/p/w500{poster}"
r = requests.get(img_url, headers=headers)

if r.status_code == 200:
    save_path = Path(f"{title}.jpg")
    save_path.write_bytes(r.content)
else:
    print("download failed")

Top10


import requests
from pathlib import Path
api_access = 'Your API Key'

page = 1
url = f"https://api.tmdb.org/3/movie/\
top_rated?language=en-US&page={page}"
headers = {
    "accept": "application/json",
    "Authorization": f"Bearer {api_access}"
}
response = requests.get(url, headers=headers)
response_dict = response.json()

movies=response_dict["results"]
top10 = movies[:10]

for movie in top10:
    poster = movie['poster_path']
    title = movie['title']
    img_url = f"https://image.tmdb.org/t/p/w500{poster}"
    r = requests.get(img_url, headers=headers)

    if r.status_code == 200:
        save_path = Path(f"{title}.jpg")
        save_path.write_bytes(r.content)
    else:
        print("download failed")

Now Playing


import requests
from pathlib import Path
api_access = 'Your API Key'

page = 1
url = f"https://api.tmdb.org/3/movie/\
now_playing?language=en-US&page={page}"
headers = {
    "accept": "application/json",
    "Authorization": f"Bearer {api_access}"
}
response = requests.get(url, headers=headers)
response_dict = response.json()

movies=response_dict["results"]
top10 = movies[:10]

for movie in top10:
    poster = movie['poster_path']
    title = movie['title']
    img_url = f"https://image.tmdb.org/t/p/w500{poster}"
    r = requests.get(img_url, headers=headers)

    if r.status_code == 200:
        save_path = Path(f"{title}.jpg")
        save_path.write_bytes(r.content)
    else:
        print("download failed")

Stock Market (股票市场)


url="http://img1.money.126.net/data/hs/kline/day/history/2022/1399001.json"
  • 代码为股票代码,上海股票前加0,如600756变成0600756,深圳股票前加1
  • 大盘指数数据查询:上证指数000001前加0,沪深300指数000300股票前加0,深证成指399001前加1,中小板指399005前加1,创业板指399006前加1
  • 是否复权,不复权为kline,复权为klinederc

贵州茅台


import requests
import matplotlib.pyplot as plt

url="http://img1.money.126.net/data/hs/kline/day/history/2022/0600519.json"
r = requests.get(url)
print(r.status_code)
response_dict = r.json() 
print(response_dict)

data = response_dict['data']

for x in data[:5]:
    print("""日期: {},开盘价:{},收盘价:{},最高价:{}
        最低价:{},交易量:{},涨幅跌幅:{}""".format(x[0],\
        x[1], x[2], x[3], x[4], x[5], x[6]))

200
日期: 20220104,开盘价:2055.0,收盘价:2051.23,最高价:2068.95
        最低价:2014.0,交易量:3354450,涨幅跌幅:0.06
日期: 20220105,开盘价:2045.0,收盘价:2022.17,最高价:2065.0
        最低价:2018.0,交易量:2804423,涨幅跌幅:-1.42
日期: 20220106,开盘价:2022.01,收盘价:1982.15,最高价:2036.0
        最低价:1938.51,交易量:5134665,涨幅跌幅:-2.07
日期: 20220107,开盘价:1975.0,收盘价:1944.0,最高价:1988.88
        最低价:1939.32,交易量:2923956,涨幅跌幅:-1.93
日期: 20220110,开盘价:1928.01,收盘价:1965.01,最高价:1977.0
        最低价:1917.55,交易量:2883270,涨幅跌幅:1.18

from datetime import datetime

date, close_p = [], []
for y in data:
    date.append(datetime.strptime(y[0],"%Y%m%d"))
    close_p.append(y[2])

fig, ax = plt.subplots()
ax.plot(date,close_p, linewidth=0.5)
ax.scatter(date,close_p, s=5)
fig.autofmt_xdate()
plt.savefig('maotai.jpg',dpi=300)

13.2 Encoding

Encoding


from pathlib import Path

path = Path('alice.txt')
contents = path.read_text(encoding='utf-8') 

Character Encoding: ASCII, Unicode, UTF-8, GBK

  • Two-dimensional code, QR code

import qrcode

img=qrcode.make("Hello!")
img.save("x.png")

import qrcode

img=qrcode.make("https://wangwanglulu.com/")
img.save("wl.png")

13.3 Textbook

  • Python Crash Couse (Chapters we do not cover: Chapter 12 - 14, 18 - 20)

    • Chapter 11: Testing Your Code

    • Chapter 12 -14: Alien Invasion

    • Chapter 18 - 20: Django

  • Python for Everybody (Chapters we do not cover: Chapter 11 - 13, 15 - 16)

    • Chapter 11: Reguler Expressions

    • Chapter 12: Networked Programs 12.4 - 12.8 (urlib, BeautifulSoup)

    • Chapter 13: Using Web Services (XML, JSON, API)

    • Chapter 15: Databases and SQL

    • Chapter 16: Visualizing data (Network, Word Cloud)

Summary

  • Web Api
    • Reading: Python Crash Course, Chapter 17