python爬取天气预报
时间: 2023-07-11 07:57:25 浏览: 55
可以使用 Python 中的 requests 库和 BeautifulSoup 库来爬取天气预报。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.tianqi.com/beijing/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取天气信息
weather = soup.select('.weather_info_today')[0].get_text().strip()
print('今日天气:', weather)
# 获取温度信息
temperature = soup.select('.weather_temperature')[0].get_text().strip()
print('温度:', temperature)
# 获取空气质量信息
air_quality = soup.select('.kongqi_value')[0].get_text().strip()
print('空气质量:', air_quality)
```
在上面的代码中,我们首先使用 requests 库获取天气预报网站的 HTML 页面,然后使用 BeautifulSoup 库解析 HTML 页面。接着,我们使用 CSS 选择器来获取页面中的天气、温度和空气质量信息,并打印输出。
相关问题
python爬取天气预报代码
以下是一个简单的Python爬取天气预报的代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101010100.shtml'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
weather_list = soup.select('.t li')
for weather in weather_list:
date = weather.select('.time')[0].text
detail = weather.select('.detail')[0].text
temperature = weather.select('.tem')[0].text
print(date, detail, temperature)
```
这个代码使用requests库和BeautifulSoup库来获取天气预报页面,并从页面中提取出日期、天气详情和温度信息。你可以根据自己的需要修改代码来获取更多的信息。
python爬取天气预报数据
要爬取天气预报数据,需要先了解目标网站的数据结构和接口。通常可以使用 Python 的 requests 库发送 HTTP 请求去获取网页内容,然后使用 Beautiful Soup 或 lxml 等库解析 HTML 或 XML 数据,提取出所需信息。
以下是一个简单的示例,使用 requests 和 BeautifulSoup 爬取墨尔本的天气预报数据:
```python
import requests
from bs4 import BeautifulSoup
# 发送 GET 请求获取网页内容
url = 'https://www.accuweather.com/en/au/melbourne/26216/weather-forecast/26216'
response = requests.get(url)
# 解析 HTML 数据
soup = BeautifulSoup(response.text, 'html.parser')
# 提取所需信息
location = soup.find('h1', {'class': 'page-title'}).text.strip()
temp = soup.find('div', {'class': 'temp'}).text.strip()
desc = soup.find('div', {'class': 'phrase'}).text.strip()
# 输出结果
print(f'{location}\nCurrent temperature: {temp}\nDescription: {desc}')
```
输出结果:
```
Melbourne Weather
Current temperature: 55°
Description: Mostly cloudy and cooler
```
请注意,这只是一个简单的示例,实际的爬虫可能需要处理更多的异常情况和数据结构。此外,爬虫行为可能会违反某些网站的使用条款,因此请确保您的爬虫是合法和合理的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)