最近看到了http://www.kidscode.cn/archives/15862
的文章
但是!!!
我没没没有xcode!!!
……
……
………………
	
 
所以很不爽,于是我就弄了一个不需要xcode的实用小程序(基本上就是翻新了一下)
	
 
第一个:天气预报
需要的组件:urllib,requests,beautifuisoup4(import时写作bs4),xpinyin
都可以通过pip安装:pip install urllib requests beautifuisoup4 xpinyin
代码:
	
 
	
 
import urllib,requests,xpinyin#导入urllib,requests,正则表达式,xpinyin和beautifulsoup4
from bs4 import BeautifulSoup 
def get_weather(city_pinyin):
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"}
    website = "https://www.tianqi.com/" + city_pinyin + "/"
    req = urllib.request.Request(url=website, headers=header)
    page = urllib.request.urlopen(req)
    html = page.read()
    soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
    nodes = soup.find_all('dd')
    tody_weather = [node.text.strip() for node in nodes]  
    tody_weather[0] = tody_weather[0][:2] 
    tianqi = " ".join([s for s in tody_weather if s.strip("\n")])
    return tianqi  # 返回结果
pinyin = xpinyin.Pinyin()
m = input('要查询的天气')
tianqi_pinyin = pinyin.get_pinyin(m,'') 
print(get_weather(tianqi_pinyin))
	
	
	
 
第二个:穿衣指南
需要的库和上面一样
都用了get_weather函数(就是上一个再拿正则表达式找一下温度字符串)
代码:
	
 
	
 
import urllib,requests,xpinyin,re#导入urllib,requests,正则表达式,xpinyin和beautifulsoup4
from bs4 import BeautifulSoup 
def get_weather(city_pinyin):
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"}
    website = "https://www.tianqi.com/" + city_pinyin + "/"
    req = urllib.request.Request(url=website, headers=header)
    page = urllib.request.urlopen(req)
    html = page.read()
    soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
    nodes = soup.find_all('dd')
    tody_weather = [node.text.strip() for node in nodes]  
    tody_weather[0] = tody_weather[0][:2] 
    tianqi = " ".join([s for s in tody_weather if s.strip("\n")])
    return tianqi  # 返回结果
wenduRegex = re.compile(r'(\S)+℃')
pinyin = xpinyin.Pinyin()
m = input('穿衣查询\n你在哪个城市:')
tianqi_pinyin = pinyin.get_pinyin(m,'') 
weather = get_weather(tianqi_pinyin)
m = wenduRegex.search(weather)
m = m.group()
print(f'温度:{m}')
m = int(m[:-1])
if m >= 30:
    print('需要短袖T恤')
elif m <= 5:
    print('需要羽绒服绒裤')
else:
    print('需要长袖长裤')
	
	
 
	
	库都是一样的(水一下)
 
代码:
	
 
	
 
	
import urllib,requests,xpinyin,re#导入urllib,requests,正则表达式,xpinyin和beautifulsoup4
from bs4 import BeautifulSoup 
def get_weather(city_pinyin):
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"}
    website = "https://www.tianqi.com/" + city_pinyin + "/"
    req = urllib.request.Request(url=website, headers=header)
    page = urllib.request.urlopen(req)
    html = page.read()
    soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
    nodes = soup.find_all('dd')
    tody_weather = [node.text.strip() for node in nodes]  
    tody_weather[0] = tody_weather[0][:2] 
    tianqi = " ".join([s for s in tody_weather if s.strip("\n")])
    return tianqi  # 返回结果
wenduRegex = re.compile(r'(\S)+℃')
pinyin = xpinyin.Pinyin()
m = input('穿衣查询\n你在哪个城市:')
tianqi_pinyin = pinyin.get_pinyin(m,'') 
weather = get_weather(tianqi_pinyin)
m = wenduRegex.search(weather)
m = m.group()
print(f'温度:{m}')
m = int(m[:-1])
if m >= 30:
    print('需要防暑')
elif m <= 15:
    print('需要防冻')
else:
    print('天气合适')
 
	
4.计算器
这个啥库都不需要
代码:
	
 
	
print('计算器Calculator')
while True:
    try:
        print('请输入算式,按下Ctrl-C退出')
        print(eval(input()))
    except KeyboardInterrupt:
        print('Exit')
        break
 
	
	
这就是这篇拙作的内容啦
感谢观看!
	
文章内容属作者个人观点,不代表本站立场,如有侵权立删。






