用python写一个简单的查看天气、新闻的程序
工具:pycharm、python3.8、火狐/谷歌浏览器
导入包:re、requests,sys
数据来源:天气—https://weather.com
新闻—凤凰网
上代码:
import re,requests
import sys
def main():
while True:
print('**************************')
print('**********YTYG************')
print('****天气---------1*********')
print('****新闻---------2*********')
print('****退出---------0*********')
print('***************************')
v = int(input('请选择对应的数字: '))
if v == 1:
weath()
elif v == 2:
New()
elif v == 0:
sys.exit(0)
else:
print('请输入正确的数字 QAQ !!')
def New():
def getnewsTitle():
response = requests.get('http://www.ifeng.com/')
response.encoding = 'utf-8'
html = response.text
reg = r'{"url":"(.*?)","title":"(.*?)","children":null}'
newstitle = re.findall(reg, html)
return newstitle
for newstitle in getnewsTitle():
newtitleUrl = newstitle[0]
newTitle = newstitle[1]
newTitles = newTitle.split('url')[0]
print(newTitles)
print(newtitleUrl)
def weath():
def mainT():
getcitylocation = input('请输入您所在城市: ')
if getcitylocation == "常德":
todaycd()
print('查询完成,继续查询请按1,返回主页请按2 ')
w = int(input('请选择对应的数字: '))
if w == 1:
weath()
elif w == 2:
main()
elif getcitylocation == "郑州":
todayzz()
print('查询完成,继续查询请按1,返回主页请按2 ')
w = int(input('请选择对应的数字: '))
if w == 1:
weath()
elif w == 2:
main()
elif getcitylocation == '北京':
todaybj()
print('查询完成,继续查询请按1,返回主页请按2 ')
w = int(input('请选择对应的数字: '))
if w == 1:
weath()
elif w == 2:
main()
else:
print('目前天气服务不支持您所在城市')
main()
def todaycd():
print('正在查询,请稍后')
print(' ... ')
def getCitycd():
response = requests.get(
'https://weather.com/zh-CN/weather/today/l/29.04,111.69?par=apple_widget&locale=zh_CN1')
response.encoding = 'utf-8'
html = response.text
reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
felttemp = re.findall(reg, html)
return felttemp
for todaytempcd in getCitycd():
print('常德今天天气: ' + todaytempcd)
def todayzz():
print('正在查询,请稍后')
print(' ... ')
def getCityzz():
response = requests.get('https://weather.com/zh-CN/weather/today/l/CHXX0165:1:CH')
response.encoding = 'utf-8'
html = response.text
reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
felttemp2 = re.findall(reg, html)
return felttemp2
for todaytempzz in getCityzz():
print('郑州今天天气: ' + todaytempzz)
def todaybj():
print('正在查询,请稍后')
print(' ... ')
def getCitybj():
response = requests.get('https://weather.com/zh-CN/weather/today/l/71ca347e2948ee9490525aa5433fa91da6973ae51ea0f765fbe8e85b9f16c5df')
response.encoding = 'utf-8'
html = response.text
reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
felttemp3 = re.findall(reg,html)
return felttemp3
for todaytempbj in getCitybj():
print('北京今天天气: ' + todaytempbj)
mainT()
main()
本博客为原创,转载请标明来源