最近看到了http://www.nikolasmelkova.com/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
這就是這篇拙作的內容啦
感謝觀看!
文章內容屬作者個人觀點,不代表本站立場,如有侵權立刪。