每次出去玩不是下雨就是暴晒,于是乎我就用python做一个天气预报,看你老天还怎么搞我
每次出去玩不是下雨就是暴晒,打开网站查询又太麻烦。正好最近正在学习python,于是乎拿来练练手!https://static.52pojie.cn/static/image/hrline/2.gif
用到的模块有 :
re:正则表达式对返回数据进行筛选(也可以使用beautiful soup)
requests:代替我们访问天气预报官网,并把数据返回
time:控制时间,温柔对待别人的网站{:1_927:}
https://static.52pojie.cn/static/image/hrline/5.gif
大概思路:(1)先找到目标网址,向这个网址发起请求并获得响应内容
(2)获得的响应内容用re模块进行筛选(beautiful soup也是可以的)
(3)查找一周后的天气状况,再次进行筛选
https://static.52pojie.cn/static/image/hrline/4.gif
下面上代码(新手,大佬还请多指点)
import re
import requests
import time
def get_url():
try:
num = input('请输入你要查询的城市:')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363',
'Cookie': 'Hm_lpvt_ab6a683aa97a52202eab5b3a9042a8d2=1593475715; Hm_lvt_ab6a683aa97a52202eab5b3a9042a8d2=1593475648; UM_distinctid=173028cca6411d1-01f7d541a39b96-71415a3a-1fa400-173028cca65a72; CNZZDATA1277722738=1358689083-1593473370-https%253A%252F%252Fwww.tianqi.com%252F%7C1593473370; CNZZDATA1275796416=466240679-1593471302-https%253A%252F%252Fwww.baidu.com%252F%7C1593471302; CNZZDATA1268732535=1963896862-1593474962-https%253A%252F%252Fwww.tianqi.com%252F%7C1593474962'
}
url = 'https://www.tianqi.com/' + num + '/'
global response
response = requests.post(url,headers = headers)
reg = r'<h2>(.*?)</h2>'
refind = re.findall(reg,response.text)
reg_time = r'<dd class="week">(.*?)</dd>'
refind_time = re.findall(reg_time,response.text)
reg_c = r'<p class="now"><b>(.*?)</b><i>.</i></p>'
refind_C = re.findall(reg_c,response.text)
reg_wter = r'<span><b>(.*?)</b>.*?</span>'
refind_wter = re.findall(reg_wter,response.text)
reg_wter_2 = r'<span><b>.*?</b>(.*?)</span>'
refind_wter_2 = re.findall(reg_wter_2,response.text)
reg_all = r'<dd class="shidu"><b>(.*?)</b><b>(.*?)</b><b>(.*?)</b></dd>'
refind_all = re.findall(reg_all,response.text)
reg_sun = r'<dd class="kongqi" ><h5 style="background-color:#79b800;">(.*?)</h5><h6>(.*?)</h6><span>(.*?)<br />(.*?)</span></dd>'
refind_sun = re.findall(reg_sun,response.text)
print('\n您现在看到的城市是:',refind,'\n')
print('\t日期:',refind_time,time.asctime())
print('\t温度:',refind_C,'℃','\t 最低温/最高温:',refind_wter_2)
print('\t天气状况:',refind_wter)
print('\t',refind_all)
print('\t',refind_all)
print('\t',refind_all)
print('\t',refind_sun,'\n\t',refind_sun,'\n\t',refind_sun,'\n\t',refind_sun)
exceptIndexError:
print('请输入具体城市名,不要输入省份!')
get_url()
def week():
global response
week1 = r'<li><b>(.*?)</b><span>(.*?)</span><img src=.*?></li>'
find_wek = re.findall(week1,response.text)
week2 = r'<li><span>(.*?)</span><b>(.*?)</b></li>'
find_wek2 = re.findall(week2,response.text)
week3 = r'<ul class="txt txt2"> *?(.*?)'
find_wek3 = re.findall(week3,response.text)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print('\t',find_wek,find_wek,'最高温:',find_wek2,'最低温:',find_wek2)
print(find_wek3)
if __name__ == '__main__':
print('\n','*'*16,'实时天气查询程序,输入时请输入该城市的拼音,没有查询到该城市则默认北京','*'*16,'\n')
print('例如: 北京 ---> beijing ')
get_url()
w = input('\n查询未来一周的天气?(是/否)')
if w == '是':
week()
else:
input('请按任意键退出...')
input('请按任意键退出...')
这里是打包好的源码文件:https://com-that.lanzouj.com/i81Kee70sod 依然是老哥们喜闻乐见的难受云
免费的评分来一波~{:301_997:} 楼主果然真性情,敢与天斗!{:301_993:}在线膜拜。 我顶!自己顶自己! 我用的是api,不得不说lz厉害{:1_921:} 我顶顶顶 不错不错 阿跃蜀黍 发表于 2020-8-30 00:29
帮你加了一个pinyin模块,直接输入中文就行
import re
import requests
厉害阿{:301_997:},点赞!
页:
[1]