使用BeautifulSoup模块对网页源代码进行解析。首先,您需要导入BeautifulSoup模块和requests模块,并使用requests模块获取网页源代码
import requests
from bs4 import BeautifulSoup
# 获取网页源代码
url = "http://example.com" # 替换为你要解析的网页的URL
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析数据
data_list = [] # 存储解析的数据
# 找到包含日期、天气状况、气温、风力风向的标签
tags = soup.find_all("div", class_="weather-info")
for tag in tags:
# 解析日期
date = tag.find("span", class_="date").text.strip()
# 解析天气状况
weather = tag.find("span", class_="weather").text.strip()
# 解析气温
temperature = tag.find("span", class_="temperature").text.strip()
# 解析风力风向
wind = tag.find("span", class_="wind").text.strip()
# 打印解析的数据
print(f"日期: {date}, 天气: {weather}, 气温: {temperature}, 风力风向: {wind}")
# 将数据添加到列表中
data_list.append((date, weather, temperature, wind))
# 打印前10条数据
print("\n前10条数据:")
for data in data_list[:10]:
print(f"日期: {data[0]}, 天气: {data[1]}, 气温: {data[2]}, 风力风向: {data[3]}")
















