接口httpx demo
Phonre.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import httpx
phone = input(f"请输入您的手机号:") headers = {"Content-Type": "application/x-www-form-urlencoded"} params = {'phone': phone, 'key': ''} url = "http://apis.juhe.cn/mobile/get" r = httpx.get(url, headers=headers, params=params)
data = r.json() print(data) if data['resultcode'] == '200' and data['error_code'] == 0: result = data['result']
brief_info = f"手机号: {params['phone']}\n" \ f"归属地: {result['province']}省 {result['city']}市\n" \ f"运营商: {result['company']}\n" \ f"区号: {result['areacode']}\n" \ f"邮编: {result['zip']}" print(brief_info) else: if data['reason'] == 'Wrong phone number!': print(f"查询失败,请输入正确的手机号!") else: print(f"查询失败,原因: {data['reason']}")
|
FlaskProject
已开源:httpx_test/ckphton_FlaskProject at main · Youli2022/httpx_test

TopNews.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import httpx
headers = {"Content-Type": "application/x-www-form-urlencoded"} params = {'key': '', 'page_size': '1'} url = "http://v.juhe.cn/toutiao/index" r = httpx.get(url, headers=headers, params=params) data = r.json() print(data)
if data.get('error_code') == 0 and data.get('result', {}).get('stat') == '1': news_list = data['result']['data']
print(f"共获取 {len(news_list)} 条新闻:\n")
for i, news in enumerate(news_list, 1): print(f"{i}. {news['title']}") print(f" 时间: {news['date']}") print(f" 来源: {news['author_name']}") print(f" 分类: {news['category']}")
if news.get('thumbnail_pic_s'): print(f" 缩略图: {news['thumbnail_pic_s']}")
print(f" 详情链接: {news['url']}\n")
else: print(f"请求失败: {data.get('reason', '未知错误')}")
|
FlaskProject
已开源:httpx_test/topNews_FlaskProject at main · Youli2022/httpx_test

SJfunning.py
1 2 3 4 5 6 7 8
| import httpx
headers = {"Content-Type": "application/x-www-form-urlencoded"} params = {'key': '',} url = "http://v.juhe.cn/joke/randJoke" r = httpx.get(url, headers=headers, params=params) data = r.json() print(data)
|
FlaskProject
已开源:httpx_test/funning_FlaskProject at main · Youli2022/httpx_test

技术栈
- Python - 后端语言
- Flask - 轻量级Web框架
- HTTPX - 现代化的HTTP客户端
- HTML5/CSS3 - 前端界面
- JavaScript - 简单动画效果
让编程更有趣,让生活更欢乐! 😄