如何在Python中从Google中提取天气数据?代码示例

2021年11月16日19:02:17 发表评论 683 次浏览

Python从Google提取天气数据?本文教你使用requests和 Beautiful Soup 在 Python 中抓取 Google 天气搜索页面以提取有用的信息,例如当前天气、温度、未来几天的天气等等。

你可能知道,网络抓取本质上是从网站中提取数据。用像 Python 这样的高级编程语言来完成这样的任务非常方便和强大。在本教程中,你将学习如何使用请求BeautifulSoupGoogle搜索引擎中抓取天气数据。

Python如何获取天气数据?虽然,这不是获取特定位置实际天气的完美和官方方式,因为有数百个天气API可供使用。但是,熟悉抓取对你来说是一个很好的练习。

还有一些已经用 Python 构建的方便的工具,例如wttr.in。查看本教程以了解如何使用它。

相关: 如何在 Python 中制作电子邮件提取器。

Python如何从Google提取天气数据?好的,让我们开始吧,安装所需的依赖项:

pip3 install requests bs4

Python从Google提取天气数据?首先,让我们做一点实验,打开一个谷歌搜索栏,输入例如:“weather london”,你会看到官方天气,让我们右击查看HTML代码,如下图所示:

如何在Python中从Google中提取天气数据?代码示例

注意:Google 没有合适的天气 API,因为它还会从 weather.com 中抓取天气数据,因此我们基本上是从它那里抓取数据。

Python如何从Google提取天气数据?你将被转发到负责显示地区、日期和小时以及实际天气的 HTML 代码:

如何在Python中从Google中提取天气数据?代码示例

太好了,让我们尝试在 Python 交互式 shell 中快速提取此信息:

In [7]: soup = BeautifulSoup(requests.get("https://www.google.com/search?q=weather+london").content)

In [8]: soup.find("div", attrs={'id': 'wob_loc'}).text
Out[8]: 'London, UK'

不要担心我们是如何创建soup对象的,你现在需要担心的是如何从 HTML 代码中获取该信息,所有你必须指定给soup.find()方法的是 HTML 标签名称和匹配的属性,在这种情况下,id为“wob_loc”的div元素将为我们提供 location 。

同样,让我们​​提取当前日期和时间:

In [9]: soup.find("div", attrs={"id": "wob_dts"}).text
Out[9]: 'Wednesday 3:00 PM'

实际天气:

In [10]: soup.find("span", attrs={"id": "wob_dc"}).text
Out[10]: 'Sunny'

复制好的,现在你已经熟悉了,让我们创建我们的快速脚本来获取有关天气的更多信息(尽可能多的信息)。打开一个新的 Python 脚本并跟随我。

Python如何获取天气数据?首先,让我们导入必要的模块:

from bs4 import BeautifulSoup as bs
import requests

值得注意的是,谷歌试图阻止我们以编程方式抓取其网站,因为它是一种非官方的获取数据的方式,因为它为我们提供了一个方便的替代方案,即自定义搜索引擎(查看本教程了解如何使用它) ,但只是出于教育目的,我们将假装我们是一个合法的网络浏览器,让我们定义用户代理:

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
# US english
LANGUAGE = "en-US,en;q=0.5"

Python从Google提取天气数据?此链接为你提供最新的浏览器版本,请确保你更换USER_AGENT为最新版本。让我们定义一个给定 URL 的函数,它尝试提取所有有用的天气信息并将其返回到字典中:

def get_weather_data(url):
    session = requests.Session()
    session.headers['User-Agent'] = USER_AGENT
    session.headers['Accept-Language'] = LANGUAGE
    session.headers['Content-Language'] = LANGUAGE
    html = session.get(url)
    # create a new soup
    soup = bs(html.text, "html.parser")

Python如何从Google提取天气数据?我们在这里所做的就是使用该浏览器和语言创建一个会话,然后使用session.get(url)从网络下载 HTML 代码,最后使用HTML 解析器创建BeautifulSoup对象。

让我们获取当前区域、天气、温度以及实际日期和时间:

    # store all results on this dictionary
    result = {}
    # extract region
    result['region'] = soup.find("div", attrs={"id": "wob_loc"}).text
    # extract temperature now
    result['temp_now'] = soup.find("span", attrs={"id": "wob_tm"}).text
    # get the day and hour now
    result['dayhour'] = soup.find("div", attrs={"id": "wob_dts"}).text
    # get the actual weather
    result['weather_now'] = soup.find("span", attrs={"id": "wob_dc"}).text

既然显示了当前的降水、湿度和风,为什么不抓住它们呢?

    # get the precipitation
    result['precipitation'] = soup.find("span", attrs={"id": "wob_pp"}).text
    # get the % of humidity
    result['humidity'] = soup.find("span", attrs={"id": "wob_hm"}).text
    # extract the wind
    result['wind'] = soup.find("span", attrs={"id": "wob_ws"}).text

Python如何获取天气数据?让我们尝试获取有关接下来几天的天气信息,如果你花一些时间查找它的 HTML 代码,你会发现类似于以下内容:

<div class="wob_df"
    style="display:inline-block;line-height:1;text-align:center;-webkit-transition-duration:200ms,200ms,200ms;-webkit-transition-property:background-image,border,font-weight;font-weight:13px;height:90px;width:73px"
    data-wob-di="3" role="button" tabindex="0" data-ved="2ahUKEwifm-6c6NrkAhUBdBQKHVbBADoQi2soAzAAegQIDBAN">
    <div class="Z1VzSb" aria-label="Saturday">Sat</div>
    <div style="display:inline-block"><img style="margin:1px 4px 0;height:48px;width:48px" alt="Sunny"
            src="//ssl.gstatic.com/onebox/weather/48/sunny.png" alt="如何在Python中从Google中提取天气数据?代码示例" data-atf="1"></div>
    <div style="font-weight:normal;line-height:15px;font-size:13px">
        <div class="vk_gy" style="display:inline-block;padding-right:5px"><span class="wob_t"
                style="display:inline">25</span><span class="wob_t" style="display:none">77</span>°</div>
        <div class="vk_lgy" style="display:inline-block"><span class="wob_t" style="display:inline">17</span><span
                class="wob_t" style="display:none">63</span>°</div>
    </div>
</div>

复制我知道不是人类可读的,但是这个父div包含有关第二天的所有信息,即“星期六”,如第一个子div元素所示,在aria-label属性中具有Z1VzSb类,天气信息在img元素中的alt属性,在本例中为"Sunny" 。但是,摄氏度华氏度都有最大值和最小值,这些代码行会处理一切:

    # get next few days' weather
    next_days = []
    days = soup.find("div", attrs={"id": "wob_dp"})
    for day in days.findAll("div", attrs={"class": "wob_df"}):
        # extract the name of the day
        day_name = day.findAll("div")[0].attrs['aria-label']
        # get weather status for that day
        weather = day.find("img").attrs["alt"]
        temp = day.findAll("span", {"class": "wob_t"})
        # maximum temparature in Celsius, use temp[1].text if you want fahrenheit
        max_temp = temp[0].text
        # minimum temparature in Celsius, use temp[3].text if you want fahrenheit
        min_temp = temp[2].text
        next_days.append({"name": day_name, "weather": weather, "max_temp": max_temp, "min_temp": min_temp})
    # append to result
    result['next_days'] = next_days
    return result

现在结果字典得到了我们需要的一切,让我们通过使用argparse解析命令行参数来完成脚本:

if __name__ == "__main__":
    URL = "https://www.google.com/search?lr=lang_en&ie=UTF-8&q=weather"
    import argparse
    parser = argparse.ArgumentParser(description="Quick Script for Extracting Weather data using Google Weather")
    parser.add_argument("region", nargs="?", help="""Region to get weather for, must be available region.
                                        Default is your current location determined by your IP Address""", default="")
    # parse arguments
    args = parser.parse_args()
    region = args.region
    URL += region
    # get data
    data = get_weather_data(URL)

显示一切:

    # print data
    print("Weather for:", data["region"])
    print("Now:", data["dayhour"])
    print(f"Temperature now: {data['temp_now']}°C")
    print("Description:", data['weather_now'])
    print("Precipitation:", data["precipitation"])
    print("Humidity:", data["humidity"])
    print("Wind:", data["wind"])
    print("Next days:")
    for dayweather in data["next_days"]:
        print("="*40, dayweather["name"], "="*40)
        print("Description:", dayweather["weather"])
        print(f"Max temperature: {dayweather['max_temp']}°C")
        print(f"Min temperature: {dayweather['min_temp']}°C")

Python从Google提取天气数据?如果你运行这个脚本,它会自动获取由你的IP 地址确定的当前区域的天气。但是,如果你想要一个不同的区域,你可以将它作为参数传递:

C:\weather-extractor>python weather.py "New York"

复制这将显示美国纽约州的天气数据:

Weather for: New York, NY, USA
Now: wednesday 2:00 PM
Temperature now: 20°C
Description: Mostly Cloudy
Precipitation: 0%
Humidity: 52%
Wind: 13 km/h
Next days:
======================================== wednesday ========================================
Description: Mostly Cloudy
Max temperature: 21°C
Min temperature: 12°C
======================================== thursday ========================================
Description: Sunny
Max temperature: 22°C
Min temperature: 14°C
======================================== friday ========================================
Description: Partly Sunny
Max temperature: 28°C
Min temperature: 18°C
======================================== saturday ========================================
Description: Sunny
Max temperature: 30°C
Min temperature: 19°C
======================================== sunday ========================================
Description: Partly Sunny
Max temperature: 29°C
Min temperature: 21°C
======================================== monday ========================================
Description: Partly Cloudy
Max temperature: 30°C
Min temperature: 19°C
======================================== tuesday ========================================
Description: Mostly Sunny
Max temperature: 26°C
Min temperature: 16°C
======================================== wednesday ========================================
Description: Mostly Sunny
Max temperature: 25°C
Min temperature: 19°C

Python如何获取天气数据?好的,我们完成了本教程,我希望这有助于你了解如何结合请求和BeautifulSoup从网页中获取数据。

顺便说一句,还有另一个教程用于在 Python 中提取 YouTube 视频数据或在 Python 中访问维基百科页面!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: