You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.8 KiB
75 lines
2.8 KiB
4 months ago
|
import requests
|
||
|
from bs4 import BeautifulSoup
|
||
|
|
||
|
def get_weather():
|
||
|
url = "https://www.weather.com.cn/textFC/db.shtml"
|
||
|
response = requests.get(url)
|
||
|
html_content = response.content
|
||
|
|
||
|
soup = BeautifulSoup(html_content, 'html.parser')
|
||
|
|
||
|
# 获取日期
|
||
|
day_tabs = soup.find('ul', class_='day_tabs')
|
||
|
days = [day.text.strip() for day in day_tabs.find_all('li')][:3] # 只取前三天
|
||
|
|
||
|
# 获取天气信息
|
||
|
weather_data = []
|
||
|
tables = soup.find_all('table', width="100%") # 找到所有天气表格
|
||
|
for table in tables:
|
||
|
rows = table.find_all('tr')
|
||
|
for row in rows:
|
||
|
cells = row.find_all('td')
|
||
|
if len(cells) >= 8: # 确保是天气数据行
|
||
|
city = cells[1].text.strip()
|
||
|
weather_info = {
|
||
|
'city': city,
|
||
|
'today': {
|
||
|
'day': {
|
||
|
'weather': cells[2].text.strip(),
|
||
|
'wind': cells[3].text.strip(),
|
||
|
'temp': cells[4].text.strip()
|
||
|
},
|
||
|
'night': {
|
||
|
'weather': cells[5].text.strip(),
|
||
|
'wind': cells[6].text.strip(),
|
||
|
'temp': cells[7].text.strip()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
# 检查是否有明天和后天的数据
|
||
|
if len(cells) >= 15:
|
||
|
weather_info['tomorrow'] = {
|
||
|
'day': {
|
||
|
'weather': cells[9].text.strip(),
|
||
|
'wind': cells[10].text.strip(),
|
||
|
'temp': cells[11].text.strip()
|
||
|
},
|
||
|
'night': {
|
||
|
'weather': cells[12].text.strip(),
|
||
|
'wind': cells[13].text.strip(),
|
||
|
'temp': cells[14].text.strip()
|
||
|
}
|
||
|
}
|
||
|
if len(cells) >= 22:
|
||
|
weather_info['day_after_tomorrow'] = {
|
||
|
'day': {
|
||
|
'weather': cells[16].text.strip(),
|
||
|
'wind': cells[17].text.strip(),
|
||
|
'temp': cells[18].text.strip()
|
||
|
},
|
||
|
'night': {
|
||
|
'weather': cells[19].text.strip(),
|
||
|
'wind': cells[20].text.strip(),
|
||
|
'temp': cells[21].text.strip()
|
||
|
}
|
||
|
}
|
||
|
weather_data.append(weather_info)
|
||
|
|
||
|
return days, weather_data
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
days, weather_data = get_weather()
|
||
|
|
||
|
print(weather_data)
|