Matchpole
Posts: 7
Joined: Wed May 22, 2013 10:49 am

urlopen

Mon Sep 02, 2013 5:32 pm

Hi All,
I have been trying to input a script from the Pi Education Manual, Lesson 3.7. I hit a problem with the first line which is as follows "from urllib.request import urlopen" then 2nd. line "from xml.dom import minidom".
I have been unable to find any references to the above in apt-cache.

This script is supposed to open an "rss" feed and download weather info from "http://open.live.bbc.co.uk/weather/feeds/en/" and get weather data from 5 locations.

Can anyone please give me the current "working" replacement for the lines above.

Thank you for your help.

Dave.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: urlopen

Mon Sep 02, 2013 8:19 pm

No 'replacement' needed to the lines of code - they are fine. It's the way you are running them...

Way back in the manual at Section 3.1 it says -
From the menu on your desktop, you should be able to find “Programming” and
then “Idle3”. Select this to open up a special Python editor for creating Python
programs, called “IDLE”.
That means that the examples are for python3, not python. If you run them with python3 the import statements return without error.

Code: Select all

pi@NOOBSv121pub ~ $ python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib.request import urlopen
>>> from xml.dom import minidom
>>> import time
>>>
(This is using Raspbian as the OS.)
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Matchpole
Posts: 7
Joined: Wed May 22, 2013 10:49 am

Re: urlopen

Thu Sep 12, 2013 10:10 am

Hi DeeJay

Thanks for the info. I actually found this myself with later scripts in the same manual. If I had been paying attention at the time, while entering commands into a script 'Idle' tries to be helpful by offering a short list of commands that you have started to enter.

urllib is not included in the list on Idle 2.7.

Likewise "sendmail" is compatible with 2.7 and 3+ but "send_message" is for Idle 3 only.

Thank you.

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: urlopen

Sat Mar 28, 2015 3:53 pm

Just a simple update on this thread.

There is a Python library called "feedparser" which can be used to directly parse an RSS/Atom response. It presents the XML response as a dictionary. This library is installed using pip. So to get the most recent weather observation for a place :

Code: Select all

import feedparser
placenumber = "2633352"  # you can find this 7 digit number by looking at the URL for a local BBC weather forecast
url = "http://open.live.bbc.co.uk/weather/feeds/en/"+placenumber+"/observations.rss"
feed = feedparser.parse(url) # fetches and decodes the RSS URL
weatherdata = feed["items"][0]   #  the weather report is the first item in the RSS feed
print(weatherdata["title"])   # some examples of the data, to see a full list of data, print(feed) or print(weatherdata)
print(weatherdata["published"])
print(weatherdata["summary"])

Return to “Troubleshooting”