python提取数据库数据_python提取日级天气数据

本文介绍了如何使用Python从数据库中提取日级别的天气数据,详细阐述了相关的方法和步骤。
摘要由CSDN通过智能技术生成

python提取数据库数据

If you are working as data scientist where you have to build some models to get the sales forecast then Weather data becomes a very important component to get the weather variables which plays as a root variables for your data model in your machine learning algorithms. You should now have a good understanding of how to scrape web pages and extract data.

如果您是数据科学家,则必须构建一些模型以获取销售预测,那么天气数据就成为获取天气变量的重要组成部分,天气变量在机器学习算法中充当数据模型的根变量。 您现在应该对如何抓取网页和提取数据有了很好的了解。

Usage Of Weather Data — Think about a company that wants to consider weather data to improve the precision of their sales forecast. This could start with a data scientist in a data lab. The data scientist might provision weather data in the lab to refine its forecasting model. Once he confirmed that weather data would impact his model, this new dataset might be checked for quality, compliance and copyrights by a data steward, and then finally, an IT professional would automate data integration, so that weather data flows close to real time in the corporate data lake and documented, into the data catalog so that it can be consumed by business analysts and business users.

天气数据的使用-考虑一家公司要考虑天气数据以提高其销售预测的准确性。 这可以从数据实验室中的数据科学家开始。 数据科学家可能会在实验室中提供天气数据以完善其预报模型。 一旦他确认天气数据会影响他的模型,就可以由数据管理员检查这个新数据集的质量,合规性和版权,然后,最终,IT专业人员将自动进行数据集成,以使天气数据流接近实时公司数据湖并将其记录在数据目录中,以便业务分析人员和业务用户可以使用它。

You can have a more siloed approach considering that IT should bring the data using data integration tools. Then the data scientists in their data lab would use a data science platform, while the office of the CDO would use a data governance framework. But how could they work as a team with a siloed approach? And who could control this disparate set of practices, tools and datasets?

考虑到IT应该使用数据集成工具带来数据,您可以采用一种更为孤立的方法。 然后,数据实验室中的数据科学家将使用数据科学平台,而CDO的办公室将使用数据治理框架。 但是他们如何以孤立的方式作为团队合作? 谁能控制这套完全不同的实践,工具和数据集?

This is what collaborative data management is all about — allowing people to work as a team to reap all the benefits of your data.

这就是协作数据管理的全部意义所在-允许人们作为一个团队工作,以收获数据的所有好处。

Image for post

There are many paid APIs which give you that specific data and they will have charged for every location. If you don’t have the sufficient project budget then you can write your own code (web scrapping code) in python to get your day level weather data for any particular location.

有很多付费的API,可以为您提供特定的数据,并且每个位置都需要付费。 如果您没有足够的项目预算,则可以使用python编写自己的代码(网络抓取代码)来获取任何特定位置的日级天气数据。

How Does Web Scraping Work? — When we scrape the web, we write code that sends a request to the server that’s hosting the page we specified. Generally, our code downloads that page’s source code, just as a browser would. But instead of displaying the page visually, it filters through the page looking for HTML elements we’ve specified, and extracting whatever content we’ve instructed it to extract.

Web爬网如何工作? 抓取Web时,我们编写代码以将请求发送到托管我们指定页面的服务器。 通常,我们的代码会像浏览器一样下载该页面的源代码。 但是,它不是在视觉上显示页面,而是在页面中进行过滤以查找我们指定HTML元素,并提取我们指示其提取的任何内容。

Downloading weather data — We now know enough to proceed with extracting information about the local weather from the National Weather Service website. The first step is to find the page we want to scrape.

下载天气数据-现在我们已经足够了解,可以从国家气象服务网站提取有关当地天气的信息。 第一步是找到我们要抓取的页面。

Image for post

Here, we are going to use MeteoGuru.uk to get the day level weather data. We will use some most common libraries in Python to write our code and will try to write in very simplistic manner that you can easily understand each line of the code.

在这里,我们将使用MeteoGuru.uk 以获取日级天气数据。 我们将使用Python中一些最常见的库来编写代码,并尝试以非常简单的方式编写代码,以便您可以轻松理解代码的每一行。

Below is the UI for London from MeteoGuru.uk

以下是MeteoGuru.uk伦敦的UI

Image for post

Using CSS Selectors to get the weather info — We can also search for items using CSS selectors. These selectors are how the CSS language allows developers to specify HTML tags to style. Here are some examples:

使用CSS选择器获取天气信息-我们还可以使用CSS选择器搜索物品。 这些选择器是CSS语言允许开发人员指定样式HTML标签的方式。 这里有些例子:

  • p a — finds all a tags inside of a p tag.

    pa -找到所有a一个的标签内p标签。

  • body p a — finds all a tags inside of a p tag inside of a body tag.

    body pa -找到所有a一个的标签内p一的标签中body标记。

  • html body — finds all body tags inside of an html tag.

    html body —在html标签内查找所有body标签。

  • p.outer-text — finds all p tags with a class of outer-text.

    p.outer-text -找出所有p标签有一类outer-text

  • p#first — finds all p tags with an id of first.

    p#first —查找所有id为first p标签。

  • body p.outer-text — finds any p tags with a class of outer-text inside of a body tag.

    body p.outer-text查找在body标签内部具有一类outer-text任何p标签。

We now know enough to download the page and start parsing it. In the below code, we:

现在我们知道足够的信息来下载页面并开始对其进行解析。 在下面的代码中,我们:

  • Download the web page containing the forecast.

    下载包含预测的网页。
  • Create a BeautifulSoup class to parse the page.

    创建BeautifulSoup类以解析页面。

Import Python Libraries — we need some python libraries which help us in the web scrapping. Python Libraries for Web Scraping

导入Python库-我们需要一些Python库来帮助我们进行网络抓取。 用于Web搜寻的Python库

  • requests — this critical library is needed to actually get the data from the web server onto your machine, and it contains some additional cool features like caching too.

    请求-这个关键库实际上是将数据从Web服务器获取到您的计算机所必需的,并且它还包含其他一些很酷的功能,例如缓存。

  • Beautiful Soup 4 — This is the library we’ve used here, and it’s designed to make filtering data based on HTML tags straightforward.

    Beautiful Soup 4-这是我们在这里使用的库,它旨在使基于HTML标签的数据过滤变得简单。

#Python program to scrape website import bs4, refrom bs4 import BeautifulSoup

#Python程序抓取网站导入bs4,从bs4重新导入BeautifulSoup

# The requests library import requests

#请求库导入请求

# Pandas is an open-source, BSD-licensed Python library providing high-performance, # easy-to-use data structures and data analysis tools for the Python programming language.import pandas as pd

#Pandas是BSD许可的开放源代码Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具。

#import datatime libraryfrom datetime import datetime

#import datatime libraryfrom datetime导入datetime

Public variables — these are the variables which will used inside the entire python code as given below -

公共变量-这些是将在下面的整个python代码中使用的变量-

#define the base urlbase_url = “https://%s.meteoguru.uk/"

#定义基本的urlbase_url =“ https://%s.meteoguru.uk/”

#define the list of months for archive weather datalst=[“may-2019”,”june-2019",”july-2019", “august-2019”,”september-2019",”october-2019",”november-2019",”december-2019",”january-2020",”february-2020",”march-2020",”april-2020",”may-2020",”june-2020",”july-2019",“august-2020”]

#定义存档天气数据的月份清单lst = [“ 2019年5月”,“ 2019年6月”,“ 2019年7月”,“ 2019年8月”,“ 2019年9月”,“ 2019年10月”,“ 11月” -2019”,“ 2019年12月”,“ 2020年1月”,“ 2020年2月”,“ 2020年3月”,“ 2020年4月”,“ 2020年5月”,“ 2020年6月”,“ 2019年7月” “,“ 2020年8月”]

Python function for web scrapping — In this function, we will pass the web URL to download/scrap the whole web page. In this weather website, there are four div which are holing the weather data such as -

用于Web抓取的Python函数—在此函数中,我们将传递Web URL以下载/抓取整个网页。 在此天气网站中,有四个div正在收集天气数据,例如-

  1. Finding all instances of a tag at once for beginning weekdays

    在工作日开始一次查找标签的所有实例
  2. Finding all instances of a tag at once for ending weekdays

    一次查找标记的所有实例以结束工作日
  3. Finding all instances of a tag at once for beginning weekend

    在周末开始时一次查找标签的所有实例
  4. Finding all instances of a tag at once for ending weekend

    一次查找标签的所有实例,以结束周末

In this function, we are using html parser for the web parsing and defining the dataframe for the data as given below -

在此功能中,我们使用html解析器进行网络解析,并为数据定义数据框,如下所示-

Image for post

#function to get weather data by url input parameterdef get_weather_data(url): #url=’https://june-2020.meteoguru.uk/' page = requests.get(url) #Parsing a page with BeautifulSoup soup = BeautifulSoup(page.content,’html.parser’) # extract region region = soup.find(“h2”, attrs={“class”: “mb-0”}).text.replace(‘Weather for ‘,’’).replace(‘, Archive’,’’) # get next few days’ weather dataframe ndf = pd.DataFrame(columns=[“region”,”date”, “day”,”weather”, “max_temp”, “min_temp”,”wind”,”humidity”]) #Use the find method, which will return a single BeautifulSoup object days = soup.find(“div”, attrs={“class”: “grid-wraper clearfix width100”}).find(“div”, attrs={“class”: “row”})

#通过URL输入参数获取天气数据的函数def get_weather_data(url):#url =' https ://june-2020.meteoguru.uk/'page = requests.get(url)#使用BeautifulSoup汤解析页面= BeautifulSoup( page.content,'html.parser')#提取区域region = soup.find(“ h2”,attrs = {“ class”:“ mb-0”})。text.replace('Weather for','') .replace(',Archive','') #获取接下来几天的天气数据帧ndf = pd.DataFrame(columns = [“ region”,“ date”,“ day”,“ weather”,“ max_temp”,“ min_temp ”,“风”,“湿度”]))#使用find方法,它将返回单个BeautifulSoup对象days = soup.find(“ div”,attrs = {“ class”:“ grid-wraper clearfix width100”}) .find(“ div”,attrs = {“ class”:“ row”})

#Finding all instances of a tag at once for beginning weekdays for day in days.findAll(“div”, attrs={“class”: “pl-0 pr-0 grid-box mb-1 rounded with-border width1_7 begining weekdays nextmonth”}): #print(day) date_name = day.find(“div”, attrs={“class”: “pl-1 pr-1 rounded background-grey-1 width100”}).text.replace(‘\t’,’’).replace(‘\n’,’’).split(‘,’) date=date_name[0] dayn=date_name[1] max_temp = day.find(“p”, attrs={“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) min_temp = day.find(“p”, attrs={“class”: “pt-1 mb-0 pb-0 center-text”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) temp= day.find(“span”, attrs={“class”: “mb-2 pt-0 mt-0 text-center width100 fz-08”}).text.replace(‘\xa0’,’’).split(‘:’) weather=temp[0] wind= temp[1].split(‘,’)[0].replace(‘mph’,’’) humidity= temp[3].replace(‘%’,’’) #append dataframe ndf=ndf.append({“region”:region,”date”: date,”day”:dayn, “weather”: weather, “max_temp”: max_temp, “min_temp”: min_temp,”wind”:wind,”humidity”:humidity},ignore_index=True) #Finding all instances of a tag at once for ending weekend for day in days.findAll(“div”, attrs={“class”: “pl-0 pr-0 grid-box mb-1 rounded with-border width1_7 ending weekend nextmonth”}): #print(day) date_name = day.find(“div”, attrs={“class”: “pl-1 pr-1 rounded background-grey-1 width100”}).text.replace(‘\t’,’’).replace(‘\n’,’’).split(‘,’) date=date_name[0] dayn=date_name[1] max_temp = day.find(“p”, attrs={“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) #max_temp = day.find(“p”, {“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’) min_temp = day.find(“p”, attrs={“class”: “pt-1 mb-0 pb-0 center-text”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) temp= day.find(“span”, attrs={“class”: “mb-2 pt-0 mt-0 text-center width100 fz-08”}).text.replace(‘\xa0’,’’).split(‘:’) weather=temp[0] #print(temp) wind= temp[1].split(‘,’)[0].replace(‘mph’,’’) humidity= temp[3].replace(‘%’,’’) #append dataframe ndf=ndf.append({“region”:region,”date”: date,”day”:dayn, “weather”: weather, “max_temp”: max_temp, “min_temp”: min_temp,”wind”:wind,”humidity”:humidity},ignore_index=True) #Finding all instances of a tag at once for beginning weekend for day in days.findAll(“div”, attrs={“class”: “pl-0 pr-0 grid-box mb-1 rounded with-border width1_7 begining weekend nextmonth”}): #print(day) date_name = day.find(“div”, attrs={“class”: “pl-1 pr-1 rounded background-grey-1 width100”}).text.replace(‘\t’,’’).replace(‘\n’,’’).split(‘,’) date=date_name[0] dayn=date_name[1] max_temp = day.find(“p”, attrs={“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) #max_temp = day.find(“p”, {“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’) min_temp = day.find(“p”, attrs={“class”: “pt-1 mb-0 pb-0 center-text”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) temp= day.find(“span”, attrs={“class”: “mb-2 pt-0 mt-0 text-center width100 fz-08”}).text.replace(‘\xa0’,’’).split(‘:’) weather=temp[0] #print(temp) wind= temp[1].split(‘,’)[0].replace(‘mph’,’’) humidity= temp[3].replace(‘%’,’’) #append dataframe ndf=ndf.append({“region”:region,”date”: date,”day”:dayn, “weather”: weather, “max_temp”: max_temp, “min_temp”: min_temp,”wind”:wind,”humidity”:humidity},ignore_index=True) #Finding all instances of a tag at once for ending weekdays for day in days.findAll(“div”, attrs={“class”: “pl-0 pr-0 grid-box mb-1 rounded with-border width1_7 ending weekdays nextmonth”}): date_name = day.find(“div”, attrs={“class”: “pl-1 pr-1 rounded background-grey-1 width100”}).text.replace(‘\t’,’’).replace(‘\n’,’’).split(‘,’) date=date_name[0] dayn=date_name[1] max_temp = day.find(“p”, attrs={“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) #max_temp = day.find(“p”, {“class”: “pt-2 mb-1 center-text big-text-1 text-center”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’) min_temp = day.find(“p”, attrs={“class”: “pt-1 mb-0 pb-0 center-text”}).text.replace(‘\xa0’,’’).replace(‘\t’,’’).replace(‘\n’,’’).replace(‘+’,’’).replace(‘°C’,’’) temp= day.find(“span”, attrs={“class”: “mb-2 pt-0 mt-0 text-center width100 fz-08”}).text.replace(‘\xa0’,’’).split(‘:’) weather=temp[0] #print(temp) wind= temp[1].split(‘,’)[0].replace(‘mph’,’’) humidity= temp[3].replace(‘%’,’’) #append dataframe ndf=ndf.append({“region”:region,”date”: date,”day”:dayn, “weather”: weather, “max_temp”: max_temp, “min_temp”: min_temp,”wind”:wind,”humidity”:humidity},ignore_index=True) #return day level weather dataframe return ndf

#一次在工作日的第几天开始查找标签的所有实例,以天为单位的天。findAll(“ div”,attrs = {“ class”:“ pl-0 pr-0网格框mb-1,以工作周开始的边框宽度1_7进行四舍五入下个月”}):#print(day)date_name = day.find(“ div”,attrs = {“ class”:“ pl-1 pr-1四舍五入背景灰色-1宽度100”)))。text.replace(' \ t','')。replace('\ n','')。split(',')date = date_name [0] dayn = date_name [1] max_temp = day.find(“ p”,attrs = { “类”:“ pt-2 mb-1中心文本big-text-1文本中心”})。text.replace('\ xa0','')。replace('\ t','')。 replace('\ n','')。replace('+','')。replace('°C','')min_temp = day.find(“ p”,attrs = {“ class”:“ pt -1 mb-0 pb-0中心文本”})。text.replace('\ xa0','')。replace('\ t','')。replace('\ n','')。 replace('+','')。replace('°C','')temp = day.find(“ span”,attrs = {“ class”:“ mb-2 pt-0 mt-0 text-center width100 fz-08”})。text.replace('\ xa0','')。split(':')weather = temp [0] wind = temp [1] .split(',')[0]。 replace('mph','')湿度= temp [3] .replace('%','')#追加数据帧ndf = ndf.append({“ region”:region,” da te”:日期,“ day”:dayn,“天气”:天气,“ max_temp”:max_temp,“ min_temp”:min_temp,“ wind”:wind,“湿度”:湿度},ignore_index = True) #查找所有实例标签一次,以为单位结束周末,以天为单位。findAll(“ div”,attrs = {“ class”:“ pl-0 pr-0网格框mb-1取整,边框宽度为1_7,下个月周末结束”)}) :#print(day)date_name = day.find(“ div”,attrs = {“ class”:“ pl-1 pr-1四舍五入的背景灰色-1宽度100”))。text.replace('\ t', '').replace('\ n','')。split(',')date = date_name [0] dayn = date_name [1] max_temp = day.find(“ p”,attrs = {“ class”: “ pt-2 mb-1中心文本big-text-1文本中心”})。text.replace('\ xa0','')。replace('\ t','')。replace('\ n','')。replace('+','')。replace('°C','')#max_temp = day.find(“ p”,{“ class”:“ pt-2 mb-1中心文本big-text-1文本中心”})。text.replace('\ xa0','')。replace('\ t','')。replace('\ n','')min_temp = day.find(“ p”,attrs = {“ class”:“ pt-1 mb-0 pb-0 center-text”})。text.replace('\ xa0','')。replace('\ t','')。replace('\ n','')。replace('+','')。replace('°C', ”)temp = day.find(“ span”,attrs = {“ class”:“ mb-2 pt-0 mt-0文本中心宽度100 fz-08”))。text.replace('\ xa0', '').split(':')weather = temp [0] #print(temp)wind = temp [1] .split(',')[0] .replace('mph','')湿度= temp [3] .replace('%','')#append数据帧ndf = ndf.append({“ region”:region,“ date”:date,“ day”:dayn,“ weather”:天气,“ max_temp” :max_temp,“ min_temp”:min_temp,“ wind”:wind,“湿度”:湿度},ignore_index = True) #一次查找标记的所有实例,以天为单位的周末开始一天。findAll(“ div”,attrs = {“ class”:“ pl-0 pr-0 grid-box mb-1,边界下限为1_7,从下个月的周末开始四舍五入”}}):#print(day)date_name = day.find(“ div”,attrs = {“ class”:“ pl-1 pr-1圆角的背景灰色-1 width100”}))。text.replace('\ t','')。replace('\ n','')。split(',' )date = date_name [0] dayn = date_name [1] max_temp = day.find(“ p”,attrs = {“ class”:“ pt-2 mb-1中心文本big-text-1文本中心”} ).text.replace('\ xa0','')。replace('\ t','')。replace('\ n','')。replace('+','')。replace(' °C','')#max_temp =day.find(“ p”,{“ class”:“ pt-2 mb-1中心文本big-text-1文本中心”})。text.replace('\ xa0','')。replace( '\ t','')。replace('\ n','')min_temp = day.find(“ p”,attrs = {“ class”:“ pt-1 mb-0 pb-0 center-text” })。text.replace('\ xa0','')。replace('\ t','')。replace('\ n','')。replace('+','')。replace( '°C','')temp = day.find(“ span”,attrs = {“ class”:“ mb-2 pt-0 mt-0文本中心宽度100 fz-08”))。text.replace( '\ xa0','')。split(':')weather = temp [0] #print(temp)wind = temp [1] .split(',')[0] .replace('mph',' ')湿度=温度[3] .replace('%','')#append数据帧ndf = ndf.append({“ region”:region,“ date”:date,“ day”:dayn,“ weather”:天气,“最大温度”:max_temp,“最小温度”:min_temp,“风”:风,“湿度”:湿度},ignore_index = True) #一次查找标记的所有实例,以天为单位结束工作日。 “ div”,attrs = {“ class”:“ pl-0 pr-0网格框mb-1,边界下限为round_1_7,下个月的下个工作日结束” :)):date_name = day.find(“ div”,attrs = {“ class”:“ pl-1 pr-1舍入后的background-grey-1 width100”})。te xt.replace('\ t','')。replace('\ n','')。split(',')date = date_name [0] dayn = date_name [1] max_temp = day.find(“ p ”,attrs = {“ class”:“ pt-2 mb-1中心文本big-text-1文本中心”})。text.replace('\ xa0','')。replace('\ t' ,'')。replace('\ n','')。replace('+','')。replace('°C','')#max_temp = day.find(“ p”,{“类”:“ pt-2 mb-1中心文本big-text-1文本中心”})。text.replace('\ xa0','')。replace('\ t','')。replace( '\ n','')min_temp = day.find(“ p”,attrs = {“ class”:“ pt-1 mb-0 pb-0 center-text”})。text.replace('\ xa0' ,'')。replace('\ t','')。replace('\ n','')。replace('+','')。replace('°C','')temp =天.find(“ span”,attrs = {“ class”:“ mb-2 pt-0 mt-0文本中心宽度100 fz-08”))。text.replace('\ xa0','')。split( ':')weather = temp [0] #print(temp)风= temp [1] .split(',')[0] .replace('mph','')湿度= temp [3] .replace( '%','')#append dataframe ndf = ndf.append({“ region”:region,“ date”:日期,“ day”:dayn,“天气”:天气,“ max_temp”:max_temp,“ min_temp” :min_temp,“风”:风,“湿度”:湿度},ignore_index = True) #return da y级天气数据帧返回ndf

Extracting all the information from the page — Now that we know how to extract each individual piece of information, we can combine our knowledge with css selectors and list comprehensions to extract everything at once from the web page and we have to generate a web url to call this method as given below -

从页面提取所有信息-现在我们知道如何提取每条信息,我们可以将我们的知识与css选择器和列表推导相结合,以一次从网页中提取所有内容,并且必须生成一个网址以调用此方法,如下所示-

if __name__ == “__main__”: #define dataframe columns with headers df=pd.DataFrame(columns=[“region”,”date”, “day”,”weather”, “max_temp”, “min_temp”,”wind”,”humidity”])

如果__name__ ==“ __main__”:#定义标题为df = pd.DataFrame(columns = [“ region”,“ date”,“ day”,“ weather”,“ max_temp”,“ min_temp”,“ wind”的数据框列,“湿度”])

#new list for testing purpose lst=[“may-2019”]

#用于测试目的的新列表lst = [“ 2019年5月”]

#for loop in case you have multiple months for ymon in lst: print(ymon) url = base_url%(ymon) print(url) # get data df = df.append(get_weather_data(url),ignore_index=True) print(df.head())

#for循环,以防在第一时间有多个ymon:print(ymon)url = base_url%(ymon)print(url)#获取数据df = df.append(get_weather_data(url),ignore_index = True)print(df 。头())

After running the code, we get the following information's -

运行代码后,我们将获得以下信息-

may-2019https://may-2019.meteoguru.uk/

五月-2019 https://may-2019.meteoguru.uk/

Image for post

Call function for multiple locations — if you want to run the same code for the multiple location then you have to create a new list to contain these locations as given below -

多个位置的调用函数-如果要为多个位置运行相同的代码,则必须创建一个新列表来包含这些位置,如下所示-

# weather location lists loc=[“England”,”Wales”,”london”] #base url for multiple locations burl=’https://%s.meteoguru.uk/%s/'

#天气位置列表loc = [“英格兰”,“威尔士”,“伦敦”]#多个位置的基本URL burl ='https://%s.meteoguru.uk/%s/'

Now, you can see the we have change the base URL also which is taken two parameters, first one %s is used for the weather month and second %s is used for the location.

现在,您可以看到我们也更改了基本URL,它也采用了两个参数,第一个%s用于天气月份,第二个%s用于位置。

# weather location lists loc=[“England”,”Wales”,”london”] #base url for multiple locations burl=’https://%s.meteoguru.uk/%s/' #for loop for the location for l in loc: #loop for the multiple month for ymon in lst: #pass parameters in the base url url = burl%(ymon,l) #print urls print(url) #append dataframe df = df.append(get_weather_data(url),ignore_index=True) #save dataframe into csv df.to_csv(“weather_Uk.csv”, index=False)

#天气位置列表loc = [“ England”,“ Wales”,“ london”]#多个位置的基本URL burl ='https://%s.meteoguru.uk/%s/'#for位置的循环l在loc中:#在lst中循环使用ymon的多个月:#在基本url中传递参数url = burl%(ymon,l)#print urls print(url)#append dataframe df = df.append(get_weather_data(url ),ignore_index = True)#将数据帧保存到csv df.to_csv(“ weather_Uk.csv”,index = False)

https://may-2019.meteoguru.uk/England/https://may-2019.meteoguru.uk/Wales/https://may-2019.meteoguru.uk/london/

https://may-2019.meteoguru.uk/英国/ https://may-2019.meteoguru.uk/威尔士/ https://may-2019.meteoguru.uk/london/

Combining our data into a Pandas Dataframe — We can now combine the data into a Pandas DataFrame and analyze it. A DataFrame is an object that can store tabular data, making data analysis easy

将我们的数据合并到一个Pandas DataFrame中-现在,我们可以将数据合并到一个Pandas DataFrame中并进行分析。 DataFrame是可以存储表格数据的对象,使数据分析变得容易

翻译自: https://medium.com/@macxima/python-extract-day-level-weather-data-364a1959bfec

python提取数据库数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值