如何在Python中从 Google 中提取天气数据?
在这篇文章中,我们将看到如何从谷歌中提取天气数据。 Google 没有自己的天气 API,它从 weather.com 获取数据并在您在 Google 上搜索时显示。因此,我们将从 Google 抓取数据。
需要的模块:
请求:请求允许您非常轻松地发送 HTTP/1.1 请求。这个模块也没有内置于Python中。要安装此类型,请在终端中输入以下命令。
pip install requests
bs4: Beautiful Soup 是一个可以轻松从网页中抓取信息的库。无论是 HTML 还是 XML 页面,以后都可以用于迭代、搜索和修改其中的数据。
方法:
- 导入模块
- 输入带有 URL 的城市名称
"https://www.google.com/search?q="+"weather"+{cityname}
- 创建请求实例并传递 URL
- 获取原始数据。
- 从汤中提取所需的数据。
- 最后,打印所需的数据。
代码的分步实现:
第 1 步:导入请求和 bs4 库
Python3
# importing the library
import requests
from bs4 import BeautifulSoup
Python3
# enter city name
city = "lucknow"
# create url
url = "https://www.google.com/search?q="+"weather"+city
# requests instance
html = requests.get(url).content
# getting raw data
soup = BeautifulSoup(html, 'html.parser')
Python3
# get the temperature
temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
# this contains time and sky description
str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
# format the data
data = str.split('\n')
time = data[0]
sky = data[1]
Python3
# list having all div tags having particular clas sname
listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
# particular list with required data
strd = listdiv[5].text
# formatting the string
pos = strd.find('Wind')
other_data = strd[pos:]
Python3
# printing all the data
print("Temperature is", temp)
print("Time: ", time)
print("Sky Description: ", sky)
print(other_data)
Python3
# importing library
import requests
from bs4 import BeautifulSoup
# enter city name
city = "lucknow"
# creating url and requests instance
url = "https://www.google.com/search?q="+"weather"+city
html = requests.get(url).content
# getting raw data
soup = BeautifulSoup(html, 'html.parser')
temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
# formatting data
data = str.split('\n')
time = data[0]
sky = data[1]
# getting all div tag
listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
strd = listdiv[5].text
# getting other required data
pos = strd.find('Wind')
other_data = strd[pos:]
# printing all data
print("Temperature is", temp)
print("Time: ", time)
print("Sky Description: ", sky)
print(other_data)
第 2 步:创建一个包含输入的城市名称的 URL,并将其传递给 get函数。
蟒蛇3
# enter city name
city = "lucknow"
# create url
url = "https://www.google.com/search?q="+"weather"+city
# requests instance
html = requests.get(url).content
# getting raw data
soup = BeautifulSoup(html, 'html.parser')
第 3 步: Soup 将返回一堆带有 HTML 标签的数据。因此,下面显示了一大块数据,我们将借助 find函数并传递标签名和类名,从中获取所有必要的数据。
iBp4i AP7Wnd”>13°C
Saturday 11:10 am
蟒蛇3
# get the temperature
temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
# this contains time and sky description
str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
# format the data
data = str.split('\n')
time = data[0]
sky = data[1]
第 4 步:这里 list1 包含具有特定类名的所有 div 标签,该列表的索引 5 包含所有其他必需的数据。
蟒蛇3
# list having all div tags having particular clas sname
listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
# particular list with required data
strd = listdiv[5].text
# formatting the string
pos = strd.find('Wind')
other_data = strd[pos:]
第 5 步:打印所有数据
蟒蛇3
# printing all the data
print("Temperature is", temp)
print("Time: ", time)
print("Sky Description: ", sky)
print(other_data)
输出:
下面是完整的实现:
蟒蛇3
# importing library
import requests
from bs4 import BeautifulSoup
# enter city name
city = "lucknow"
# creating url and requests instance
url = "https://www.google.com/search?q="+"weather"+city
html = requests.get(url).content
# getting raw data
soup = BeautifulSoup(html, 'html.parser')
temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
# formatting data
data = str.split('\n')
time = data[0]
sky = data[1]
# getting all div tag
listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
strd = listdiv[5].text
# getting other required data
pos = strd.find('Wind')
other_data = strd[pos:]
# printing all data
print("Temperature is", temp)
print("Time: ", time)
print("Sky Description: ", sky)
print(other_data)
输出: