官术网_书友最值得收藏!

Fetching data from the weather website

We are going to add a class named Request that will be responsible for getting the data from the weather website. Let's add a file named request.py in the weatherterm/core directory with the following content:

import os
from selenium import webdriver


class Request:
def __init__(self, base_url):
self._phantomjs_path = os.path.join(os.curdir,
'phantomjs/bin/phantomjs')
self._base_url = base_url
self._driver = webdriver.PhantomJS(self._phantomjs_path)

def fetch_data(self, forecast, area):
url = self._base_url.format(forecast=forecast, area=area)
self._driver.get(url)

if self._driver.title == '404 Not Found':
error_message = ('Could not find the area that you '
'searching for')
raise Exception(error_message)

return self._driver.page_source

This class is very simple; the initializer defines the base URL and creates a PhantomJS driver, using the path where PhantomJS is installed. The fetch_data method formats the URL, adding the forecast option and the area. After that, the webdriver performs a request and returns the page source. If the title of the markup returned is 404 Not Found, it will raise an exception. Unfortunately, Selenium doesn't provide a proper way of getting the HTTP Status code; this would have been much better than comparing strings.

You may notice that I prefix some of the class properties with an underscore sign. I usually do that to show that the underlying property is private and shouldn't be set outside the class. In Python, there is no need to do that because there's no way to set private or public properties; however, I like it because I can clearly show my intent.

Now, we can import it in the __init__.py  file in the weatherterm/core directory:

from .request import Request

Now we have a parser loader to load any parser that we drop into the directory weatherterm/parsers,  we have a class representing the forecast model, and an enumeration ForecastType so we can specify which type of forecast we are parsing. The enumeration represents temperature units and utility functions to convert temperatures from Fahrenheit to Celsius and Celsius to Fahrenheit. So now, we should be ready to create the application's entry point to receive all the arguments passed by the user, run the parser, and present the data on the terminal.

主站蜘蛛池模板: 松滋市| 嘉峪关市| 宁武县| 博罗县| 蒙山县| 商水县| 乐平市| 大同县| 玛纳斯县| 阿克陶县| 美姑县| 宣威市| 新安县| 讷河市| 吴旗县| 大安市| 双辽市| 金阳县| 大埔区| 安吉县| 巧家县| 抚松县| 新乡市| 越西县| 万山特区| 鹤岗市| 安徽省| 琼海市| 榆树市| 高唐县| 仁寿县| 荃湾区| 集贤县| 赤城县| 嘉荫县| 汉沽区| 桓仁| 安图县| 五峰| 观塘区| 读书|