Python爬取天气(超简单)

it2025-10-08  3

页面源码分析

# 爬取天气 import urllib.request from bs4 import BeautifulSoup # 下载数据 url = "http://www.weather.com.cn/weather/101250101.shtml" content = urllib.request.urlopen(url).read() soup = BeautifulSoup(content, "html.parser") content = "" name = soup.find_all(attrs={"class": "sky skyid lv1 on"}) for u in name: wea = u.find(attrs={"class": "wea"}).get_text() tem = u.find(attrs={"class": "tem"}).get_text() win = u.find(attrs={"class": "win"}).get_text() content = "天气:" + wea + " 温度:" + tem + " 风速:" + win content = content.replace("\n", "") print(content)
最新回复(0)