是否有一个天气网站,我可以在给定城市名称的情况下抓取它?
Is there a weather website that i can scrape given a city name?
我在一家小公司的招聘过程中收到了一份奇怪的家庭作业。
他们要求我在不使用任何 API.
的情况下显示世界上任何位置的天气元数据,例如风速和温度
所以,我现在看到的唯一选择是抓取 HTML 网站。我没有找到任何可以仅用城市名称 抓取 的网站。唯一容易抓取但需要国家代码的是 wunderground.com .
有人知道这样的网站吗?或者也许可以推荐另一种方法来解决这个问题。
我正在使用 Python 的 BeautifulSoup 库,但我当然会使用您推荐的任何其他 language/technology。
谢谢;)
我喜欢他评论中 accdias 的解决方案,但要查看其他选项,您可以使用 selenium 模拟浏览 wunderweather 网站:
另请注意,我包含了一个名为 choice
的软件包,找到 here 进行安装。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time
import choice
url = 'https://www.wunderground.com/'
city = input('Type a city: ')
driver = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
driver.get(url)
driver.find_element_by_name('query').send_keys(city)
time.sleep(10)
soup = BeautifulSoup(driver.page_source, 'html.parser')
possible_cities = soup.find_all('span', {'class':'ui-autocomplete-term'})
choose_city = [ x.parent.text for x in possible_cities ]
city = choice.Menu(choose_city).ask()
driver.find_element_by_name('query').send_keys(Keys.CONTROL + "a")
driver.find_element_by_name('query').send_keys(Keys.DELETE)
driver.find_element_by_name('query').send_keys(city)
driver.find_element_by_name('query').send_keys(Keys.ENTER)
soup = BeautifulSoup(driver.page_source, 'html.parser')
temp = soup.find('div', {'class':'condition-data'})
wind = soup.find('div', {'class':'condition-wind'})
deg = temp.find('span',{'class':'ng-star-inserted'}).text.split()[-1]
hi = temp.find('span',{'class':'hi'}).text
low = temp.find('span',{'class':'lo'}).text
current = temp.find('span',{'class':'wu-value wu-value-to'}).text
wdesc = wind.find('p',{'class':'ng-star-inserted'}).text.strip()
print ('%s\nHi: %s%s Low: %s%s Current: %s%s\n%s' %(city, hi,deg, low,deg, current,deg, wdesc))
driver.close()
输出:
Type a city: paris
Make a choice:
0: Paris, France
1: Paris, TX
2: Paris, TN
3: Paris, KY
4: Paris, IL
5: Paris, AR
6: Parish, NY
7: Paris, MI
8: Paris, MO
9: Paris, OH
Enter number or name; return for next page
? 0
Paris, France
Hi: 44°F Low: 32°F Current: 43F
Gusts 5 mph
我在一家小公司的招聘过程中收到了一份奇怪的家庭作业。 他们要求我在不使用任何 API.
的情况下显示世界上任何位置的天气元数据,例如风速和温度所以,我现在看到的唯一选择是抓取 HTML 网站。我没有找到任何可以仅用城市名称 抓取 的网站。唯一容易抓取但需要国家代码的是 wunderground.com .
有人知道这样的网站吗?或者也许可以推荐另一种方法来解决这个问题。
我正在使用 Python 的 BeautifulSoup 库,但我当然会使用您推荐的任何其他 language/technology。
谢谢;)
我喜欢他评论中 accdias 的解决方案,但要查看其他选项,您可以使用 selenium 模拟浏览 wunderweather 网站:
另请注意,我包含了一个名为 choice
的软件包,找到 here 进行安装。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time
import choice
url = 'https://www.wunderground.com/'
city = input('Type a city: ')
driver = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
driver.get(url)
driver.find_element_by_name('query').send_keys(city)
time.sleep(10)
soup = BeautifulSoup(driver.page_source, 'html.parser')
possible_cities = soup.find_all('span', {'class':'ui-autocomplete-term'})
choose_city = [ x.parent.text for x in possible_cities ]
city = choice.Menu(choose_city).ask()
driver.find_element_by_name('query').send_keys(Keys.CONTROL + "a")
driver.find_element_by_name('query').send_keys(Keys.DELETE)
driver.find_element_by_name('query').send_keys(city)
driver.find_element_by_name('query').send_keys(Keys.ENTER)
soup = BeautifulSoup(driver.page_source, 'html.parser')
temp = soup.find('div', {'class':'condition-data'})
wind = soup.find('div', {'class':'condition-wind'})
deg = temp.find('span',{'class':'ng-star-inserted'}).text.split()[-1]
hi = temp.find('span',{'class':'hi'}).text
low = temp.find('span',{'class':'lo'}).text
current = temp.find('span',{'class':'wu-value wu-value-to'}).text
wdesc = wind.find('p',{'class':'ng-star-inserted'}).text.strip()
print ('%s\nHi: %s%s Low: %s%s Current: %s%s\n%s' %(city, hi,deg, low,deg, current,deg, wdesc))
driver.close()
输出:
Type a city: paris
Make a choice:
0: Paris, France
1: Paris, TX
2: Paris, TN
3: Paris, KY
4: Paris, IL
5: Paris, AR
6: Parish, NY
7: Paris, MI
8: Paris, MO
9: Paris, OH
Enter number or name; return for next page
? 0
Paris, France
Hi: 44°F Low: 32°F Current: 43F
Gusts 5 mph