当前位置:首页 » 编程语言 » python天气

python天气

发布时间: 2022-02-06 00:46:23

python怎么自动抓取网页上每日天气预报

使用到了urllib库和bs4。bs4提供了专门针对html的解析功能,比用RE方便许多。
# coding : UTF-8import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )from bs4 import BeautifulSoupimport csvimport urllibdef get_html(url):
html = urllib.urlopen(url) return html.read()def get_data(html_text):
final = []
bs = BeautifulSoup(html_text, "html.parser")
body = bs.body
data = body.find('div', {'id': '7d'})
ul = data.find('ul')
li = ul.find_all('li') for day in li:
temp = []
date = day.find('h1').string
temp.append(date)
inf = day.find_all('p')
temp.append(inf[0].string,) if inf[1].find('span') is None:
temperature_highest = None
else:
temperature_highest = inf[1].find('span').string
temperature_highest = temperature_highest.replace('C', '')
temperature_lowest = inf[1].find('i').string
temperature_lowest = temperature_lowest.replace('C', '')
temp.append(temperature_highest)
temp.append(temperature_lowest)
final.append(temp) return finaldef write_data(data, name):
file_name = name with open(file_name, 'a') as f:
f_csv = csv.writer(f)
f_csv.writerows(data)if __name__ == '__main__':
html_doc = get_html('http://www.weather.com.cn/weather/101190401.shtml')
result = get_data(html_doc)
write_data(result, 'weather.csv') print

运行结果保存在csv文件中,如下:
28日(今天),小雨,,13℃29日(明天),小雨转阴,15℃,12℃30日(后天),多云,19℃,14℃31日(周一),小雨,16℃,14℃1日(周二),阴转多云,16℃,10℃2日(周三),多云转晴,17℃,10℃3日(周四),多云转晴,18℃,11℃1234567

㈡ Python获取本土的天气和任意城市的天气怎么解决

返回的数据是经过gzip压缩的,如果你用urllib,需要先把获取的二进制数据解压,再解码成字符串,包括编码都帮你自动解决,不需要自己操心。

㈢ python求助 一段文字比如s=''' 天气,真好。啊;(换行)天气,真,不好 用replace

import re
print re.sub(r'[;。,\n]', ' ', string)

㈣ 如何利用python爬取某个地方1年的天气

先要找到提供这个地方天气信息的网站
然后用firefox之类的浏览器分析
之后用python按分析的结果来提取所需要的数据

㈤ 求助:用python获取天气预报

# 获取温度、湿度、风力等
WEATHER_URL_A = "http://www.weather.com.cn/data/sk/%s.html"

# 获取天气状况、最大/小温度等
WEATHER_URL_B = "http://www.weather.com.cn/data/cityinfo/%s.html"

# 获取未来7天天气数据
WEATHER_URL_C = "http://www.weather.com.cn/weather/%s.shtml"

URL里%s指城市对应的代码。详细参考:
http://www.cnblogs.com/toosuo/p/3868004.html
不过这篇文章里有的接口已经不能用了。
上面我给的三个URL里,前两个直接返回json格式数据;第三个返回是一个页面,需要自己从页面里提取想要的信息。

㈥ python爬虫完成了天气数据的爬寻,然后可以做一些什么变复杂或者变完整

再写个发邮件模块,根据爬取的天气内容判断,提醒用户是否需要带伞,适合穿什么衣服出门!

㈦ 如何利用python给手机发天气信息

分两步走:

  1. 从天气网站上抓取所要的天气数据

  2. 调用第三方提供的短信接口发送所抓取的天气数据

㈧ 如何使用python利用api获取天气预报

Python实现从网络API获取天气的方法
代码如下:
__author__ = 'saint'
import os
import urllib.request
import urllib.parse
import json
class weather(object):
# 获取城市代码的uri
code_uri = "http://apistore..com/microservice/cityinfo?cityname="
# 获取天气信息的uri
weather_uri = "http://apistore..com/microservice/weather?cityid="
# 主处理逻辑
def mainHandle(self):
print("输入你要查询的天气:")
city_name = input()
uri = self.code_uri + urllib.parse.quote(city_name)
ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
if ret['errNum'] != 0:
print(ret['retMsg'])
return False
else:
weather_uri = self.weather_uri + ret['retData']['cityCode']
data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
if data['errNum'] == 0:
ret_data = data['retData']
output = "城市名:" + city_name + "\r\n"
output += "更新时间:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
output += "天气:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
output += "当前温度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
print(output)
return True
else:
print(data['errMsg'])
return False
if __name__ == "__main__":
weather = weather()
weather.mainHandle()

㈨ 求助,Python 查天气代码问题

weatherinfo=r.json() #在json后面加上括号才能返回结果。否则只能返回函数地址。
以下python3通过:
import requests
ApiUrl="http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json()
print (weatherinfo["weatherinfo"]["ptime"])
print (weatherinfo["weatherinfo"]["temp2"])
>>>08:00
>>>5℃

㈩ 用python编写的获取天气预报的代码总是有错误,求解

weatherinfo=r.json() #在json后面加上括号才能返回结果。否则只能返回函数地址。

以下python3通过:

importrequests
ApiUrl="http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json()
print(weatherinfo["weatherinfo"]["ptime"])
print(weatherinfo["weatherinfo"]["temp2"])

>>>08:00

>>>5℃

热点内容
war3存储空间不足 发布:2024-04-28 13:20:54 浏览:948
微信密码已经忘记了如何找回 发布:2024-04-28 11:54:13 浏览:304
腾讯云服务器可以备案几个网站 发布:2024-04-28 11:54:12 浏览:458
影响编译速度的因素有哪些 发布:2024-04-28 11:53:58 浏览:255
安全配置汽车有哪些 发布:2024-04-28 11:48:07 浏览:830
存储链路中断 发布:2024-04-28 11:11:39 浏览:734
安卓锤子手机怎么改文字 发布:2024-04-28 11:09:52 浏览:100
列举贪心算法 发布:2024-04-28 10:44:15 浏览:698
文具店疫情演练脚本 发布:2024-04-28 10:23:40 浏览:25
绿色商城源码 发布:2024-04-28 10:04:45 浏览:587