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)
# Please install OpenAI SDK first: `pip3 install openai`
from openai import OpenAI
client = OpenAI(api_key="your api_key", base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "鲁迅暴打周树人"},
],
stream=False
)
print(response.choices[0].message.content)
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
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")
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")
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")
import akshare as ak
stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol="600519", period="daily",\
start_date="20170301", end_date='20240528', adjust="")
print(stock_zh_a_hist_df)
import matplotlib.pyplot as plt
from datetime import datetime
fig, ax = plt.subplots()
ax.plot(stock_zh_a_hist_df["日期"],stock_zh_a_hist_df["收盘"], linewidth=0.5)
ax.scatter(stock_zh_a_hist_df["日期"],stock_zh_a_hist_df["收盘"], s=5)
fig.autofmt_xdate()
plt.savefig('maotai.jpg',dpi=300)
GitHub 是一个基于 Git 版本控制系统 的 代码托管平台,由 GitHub 公司(现为 Microsoft 子公司)于 2008 年推出。它是全球最受欢迎的开源协作平台,广泛应用于软件开发、版本管理、团队协作和项目发布。GitHub官网地址:https://github.com
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
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: 20566130
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: 339656
Repository: https://github.com/public-apis/public-apis
Created: 2016-03-20T23:49:42Z
Updated: 2025-05-16T06:51:23Z
Description: A collective list of free APIs
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')
尽管 ChatGPT或者Deepseek 可以编写完整的代码,但是要与集成开发环境(Integrated DevelopmentEnvironment,IDE)无缝对按,使用ChatGPT 就不太方便了,尤其是在生成片段代码时,如补全一个函数的定义、补全某个语句等,在这种情况下,使用GitHub Copilot 是一个非常好的选择。当然,最好是将 ChatGPT 与 GitHub Copilot一起使用:使用 ChatGPT 生成一个完整的解决方案,并使用 GitHub Copilot 对这个解决方案进行微调。
1. 自动补全注释(按Tab键)
# 编写一个程序,读取文件夹中的文件
2. 根据函数名自动生成代码(按Tab键)
def bubblesort()
3. 生成测试用例(在bubblesort函数下方输入)
#测试bubblesort函数
4. 逐步代码生成
#定义5个列表变量,每个列表包含2到10个元素
#将5个列表合并,再调用bubblesort函数对列表排序
5. 自动生成语句架构
for i
-if
6. 生成多个候选解决方案
如果使用Tab键生成解决方案,那么对于一些复杂的代码,可能需要一行一行地生成(需 要不断地按Enter 键和Tab 键),比较麻烦。GitHub Copilot 提供了生成多个候选解决方案的功能,具体的做法就是在注释中按 Ctrl + Enter 组合键,这将显示一个新的选项卡,默认会自动生成10个解决方案。(有时候还不如直接ChatGPT或者Deepseek)
#用Flask实现一个服务端程序,只支持GET请求,请求的参数是一个字符串,返回一个字符串。
7. 检查代码漏洞
# 检查上面的代码是否有漏洞
from pathlib import Path
path = Path('alice.txt')
contents = path.read_text(encoding='utf-8')
Character Encoding: ASCII, Unicode, UTF-8, GBK
chinese = '你好'.encode('utf-8')
print(chinese) # 输出:b'\xe4\xbd\xa0\xe5\xa5\xbd'
#这是字节串(是bytes,不是字符串!)用16进制是为了方便阅读,\x是16进制的前缀
print(len(chinese)) # 输出 6(每个汉字占 3 字节)
b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode('utf-8') # 输出:你好
import qrcode
img=qrcode.make("Hello!")
img.save("x.png")
import qrcode
img=qrcode.make("https://wangwanglulu.com/")
img.save("wl.png")
Python Crash Couse (Chapter 12 - 14, 18 - 20)
Chapter 11: Testing Your Code(测试代码)
Chapter 12 -14: Alien Invasion(外星人入侵)
Chapter 18 - 20: Django(Web应用:创建网站)
Python for Everybody (Chapter 11 - 13, 15 - 16)
Chapter 11: Regular Expressions(正则表达式)
Chapter 12: Networked Programs 12.4 - 12.8 (urlib, BeautifulSoup)(分析网页)
Chapter 13: Using Web Services (XML, JSON, API)(还是web api)
Chapter 15: Databases and SQL(数据库)
Chapter 16: Visualizing data (Network, Word Cloud)(可视化:网络图,词云)