承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601
当前位置:首页  >  软件开发  >  python 爬广州天气

python 爬广州天气

管理员 2023-08-25 08:02:18 软件开发 0 ℃ 0 评论 1247字 收藏

python 爬广州天气

Python是一种简单易学的编程语言,而爬虫是Python的重要利用领域之一。今天,我们就来介绍怎样使用Python爬取广州天气。

import requests
from bs4 import BeautifulSoup
url = "http://www.weather.com.cn/weather/101280101.shtml"
res = requests.get(url)
res.encoding='UTF⑻'
soup = BeautifulSoup(res.text,"html.parser")
weather_list = soup.select(".t ul li")
for weather in weather_list:
date = weather.select(".h1")[0].text
weather_type = weather.select(".wea")[0].text
max_temperature = weather.select(".tem span")[0].text
min_temperature = weather.select(".tem i")[0].text
print("%s %s,最高温度:%s℃,最低温度:%s℃" % (date, weather_type, max_temperature, min_temperature))

上述代码利用requests和BeautifulSoup库获得广州天气预报数据,并遍历每天的天气情况,分别获得日期、天气类型、最高温度和最低温度,并输出到控制台。

运行代码后,你会发现输出了未来七天的广州天气预报,包括日期、天气类型、最高温度和最低温度等信息。

这就是怎样使用Python爬取广州天气的方法,可以为我们平常生活提供更便捷的天气查询方式。

文章来源:丸子建站

文章标题:python 爬广州天气

https://www.wanzijz.com/view/74132.html

TAG: php教程 centos
X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信