Posts

Showing posts from February, 2018

[FreeBSD] Installing and using Python's virtualenv using Python3

1. Installing Python3. $ sudo pkg install python36 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 2 package(s) will be affected (of 0 checked): New packages to be INSTALLED: python36: 3.6.4 libffi: 3.2.1_2 Number of packages to be installed: 2 The process will require 103 MiB more space. 15 MiB to be downloaded. Proceed with this action? [y/N]: y [1/2] Fetching python36-3.6.4.txz: 100% 15 MiB 15.5MB/s 00:01 [2/2] Fetching libffi-3.2.1_2.txz: 100% 34 KiB 35.3kB/s 00:01 Checking integrity... done (0 conflicting) [1/2] Installing libffi-3.2.1_2... [1/2] Extracting libffi-3.2.1_2: 100% [2/2] Installing python36-3.6.4... Extracting python36-3.6.4: 100% Message from python36-3.6.4: =========================================================================== Note that some standard Python modules are provided as separate ports as they require additional dependencies. They are availabl...

[Python] Using Python to get weather data

Here's a simple Python code snippet for finding the weather. 1. #!/usr/bin/python3 # -*- coding: utf-8 -*- import urllib.request from contextlib import closing import xml.etree.ElementTree as ET rssurl = 'https://rss-weather.yahoo.co.jp/rss/days/4410.xml' with closing(urllib.request.urlopen(rssurl)) as res: xml = res.read() tree = ET.fromstring(xml) root = tree.findall('.') for item in root[0].iter('item'): title = item.find('title').text print(title[:title.index(' - Y')]) 2. import requests import json def get_weather(): url = 'http://weather.livedoor.com/forecast/webservice/json/v1' payload = {'city': '130010'} data = requests.get(url, params = payload).json() #resp = urllib2.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read() #resp = json.loads(resp) print(data['description']['text']) ...