apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

urllib2

Mon Jun 24, 2013 2:57 am

can someone exsplain to me why urllib2 runs fine in idle but not in ldle 3?
i want to know because my first python script(new to python) runs rine in idle 3 but it wont work when it gets down to urllib2 it gives me an error but in idle its fine but i cant even get to where urllib2 is because it wont even run the first script

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: urllib2

Mon Jun 24, 2013 8:38 am

You should always include the error, then we are more likely to be able to answer.,

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Mon Jun 24, 2013 3:11 pm

heres the error i get this error in idle the thing about it is that it works fine in idle 3 its the first line
idle error
Traceback (most recent call last):
File "/home/pi/Desktop/Scripts/hello2.py", line 3, in <module>
username = input("Hello, I'm Raspberry Pi! What is your name?")
File "<string>", line 1, in <module>
NameError: name 'grant' is not defined

and the idle 3 error whats funny is the script runs fine tell it trys to pull the weather and while im at it i have an ok weather source but does anyone know of anyother weather sources that i could pull into python i have tried python-weather api but i get module isnt installed even though i went through all the steps outlined to install it

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: urllib2

Mon Jun 24, 2013 4:51 pm

Could you post the entire code, it'll make it easier to help. And paste it between the Code tags so it looks like this..

Code: Select all

print("This is some Python code!")
sys.exit(0)
As for the weather, I've used the Yahoo RSS weather feed with the feedparser module. I can give you some source to play with but it'll be weekend before I'm back on the Pi unless I get a spare half hour.

Dave
Apple say... Monkey do !!

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Mon Jun 24, 2013 5:50 pm

davef21370 wrote:Could you post the entire code, it'll make it easier to help. And paste it between the Code tags so it looks like this..

Code: Select all

print("This is some Python code!")
sys.exit(0)
As for the weather, I've used the Yahoo RSS weather feed with the feedparser module. I can give you some source to play with but it'll be weekend before I'm back on the Pi unless I get a spare half hour.

Dave
i would love to try out Yahoo RSS weather feed but will look into feedparser never heard of it(new to coding)
as for the the code here you go

Code: Select all

#my first Python Program

username = input("Hello, I'm Raspberry Pi! What is your name?")
print ('nice to meet you '+username+' hive a nice day!')

answer = input ("How are you today?")
if answer == "good":
    print ('That is good that you feel '+answer+' I hope the rest of your day goes good!')
else:
    if answer == "great":
        print ('That is great that you feel '+answer+' I hope the rest of your day goes great!')
    else:	
        print ('That is '+answer+' that you feel '+answer+' I hope the rest of your day goes great!')
        
answer = input ("How old are you?")
print('that is cool that you are '+answer+' I am only 1 and a half years old')

answer = input ("What's you favorite thing to do?")
print ('That is is cool I like to '+answer+' too! my favorite thing to do is to be used as a computer as that is what i was ment to do. By the way I was kidding i dont like to '+answer+' because I am a computer!')

answer = input ("who is your best friend?")
print ('i would like to meet '+answer+' some day')

answer = input ("do you want to know the weather?")
if answer == "no":
    print ('ok')
else:
    print ('thinking')
    from json import load
    from urllib import urlopen
    from pprint import pprint

    data = urlopen('http://openweathermap.org/data/2.1/find/name?q=urbandale')
    cities = load(data)
    if cities['count'] > 0:
       city = cities['list'][0]
       pprint(city['main'])
       pprint(city['weather'])


name = input ("are you owner of this computer? I forgot! What is your name?") 
if name  == "grant":
    print ('since your say you are grant you can use this computer!')
else:	
    print ('You are not the owner of this computer, so, '+name+', you better get off of this computer!')
    

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: urllib2

Mon Jun 24, 2013 6:08 pm

Not sure and I don't use Python3 so I may be clutching at straws but it could be something to do with the use of quotation marks and apostrophes. There are some strings contained within quotes that have apostrophes in them like "Hello, I'm Raspberry Pi, etc.", could the apostrophe in the word I'm be closing the string?

Does that make sense?

Dave
Apple say... Monkey do !!

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Mon Jun 24, 2013 6:12 pm

urllib2 is merged into urllib in python3

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Mon Jun 24, 2013 6:21 pm

davef21370 wrote:Not sure and I don't use Python3 so I may be clutching at straws but it could be something to do with the use of quotation marks and apostrophes. There are some strings contained within quotes that have apostrophes in them like "Hello, I'm Raspberry Pi, etc.", could the apostrophe in the word I'm be closing the string?

Does that make sense?

Dave
if i try and run it in idle not idle 3 i get the trackback error i posted in my second post and as for the feeds thing i copied one from another sited just google feedparser and yahoo weather rss feed and it says no moddule named feedparser how do i go about setting it up?

EDIT just got it installed still says module name feedparser not found would really like to use this same error i get for trying to run

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Mon Jun 24, 2013 6:35 pm

robbes wrote:urllib2 is merged into urllib in python3
ok i got urllib to run fine but now it wont run urlopen

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Mon Jun 24, 2013 7:22 pm

I think it depends on how you imported the urllib module.
Try something like data = urllib.urlopen('....')

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Mon Jun 24, 2013 7:39 pm

Looking a little deeper into this, see
http://stackoverflow.com/questions/2792 ... ame-urllib
Apparently you should use
from urllib.request import urlopen
as your import statement

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Mon Jun 24, 2013 8:12 pm

robbes wrote:Looking a little deeper into this, see
http://stackoverflow.com/questions/2792 ... ame-urllib
Apparently you should use
from urllib.request import urlopen
as your import statement
i changed it how you said but now am getting this error

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/Scripts/hello2.py", line 34, in <module>
    cities = load(data)
  File "/usr/lib/python3.2/json/__init__.py", line 264, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.2/json/__init__.py", line 309, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.2/json/decoder.py", line 353, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: can't use a string pattern on a bytes-like object

and do you know why in python 3 i get an error when trying to use the python-weather-api
i get an error saying that module pywapi is not found but installed as the steps form this site https://code.google.com/p/python-weather-api/ said do you know of any other alternatives im currently using open weather map it gives correct weather conditions but it gives me a wierd temputer in a wierd format have tried like 10 diffrent weather api and they all return
import error module something weather was not found

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Mon Jun 24, 2013 8:49 pm

I think part of your problem is that you have imported some modules into Python2 and some into Python3. You would probably be better off just having one of the two installed at this point.

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Mon Jun 24, 2013 9:00 pm

how do i find a list of installed modules and how do unistall them
and when i install a module how do install it to both python 2 and 3

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Mon Jun 24, 2013 9:43 pm

pip is a tool to install or uninstall packages from pypi
You can install pip itself with
sudo apt-get install python-pip
See https://pypi.python.org/pypi/pip/ for usage details.

yolk is a program which allows you find which packages (and versions) you have installed.
You can install yolk itself with
pip install yolk
To see all the installed packages, use
yolk -l

I don't think you can install a package to both Python2 and Python3 in a single step. Each is installed with its own set of environmental variables. You should actually be installing each in a virtual environment using the virtualenv tool, but that is another issue altogether.

I hope others, who very likely know a lot more about this than I do, will chime in.

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Tue Jun 25, 2013 12:38 am

i got it to work fine still having a problem though now im getting this error

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/Scripts/weather.py", line 9, in <module>
    print (forecast) ['forecast']['txt_forecast']
TypeError: 'instancemethod' object has no attribute '__getitem__'
when running this script

Code: Select all

import requests

key = '015098ae0bc15cce'
ApiUrl = \
'http://api.wunderground.com/api/' + key + '/forecast/q/IA/Urbandale.json'

r = requests.get(ApiUrl)
forecast = r.json
print forecast ['forecast'][txt_forecast] 

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Tue Jun 25, 2013 2:24 am

I think you just need to check the syntax of your print statement.

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Tue Jun 25, 2013 2:47 pm

Confused by what you mean by that. :?:

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Tue Jun 25, 2013 4:58 pm

If this is python3, print is a function and your code should be print(forecast).
I'm interested in knowing what ['forecast'][txt_forecast] is doing following the print function.

Going back a bit, you might consider using the urllib.request.urlretrieve function rather than urllib.request.urlopen
This would make it easier just to use python string manipulation tools to extract whatever content you want from the weather site, since it downloads the content to a file. That is,

Code: Select all

from urllib.request import urlretrieve
and then

Code: Select all

data = urlretrieve('your_weather_site','your_file_handle')

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Tue Jun 25, 2013 8:47 pm

what would be the file handle could you type out the whole thing how it look like i provided the api key in one of my posts so you can try it ur self

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Tue Jun 25, 2013 9:18 pm

I just meant you can put in any path and filename you want and .urlretrieve will put the data from the website there. For instance, in this code the data will be placed in your home directory in a file named weatherfile.txt:

Code: Select all

from urllib.request import urlretrieve

Code: Select all

urlretrieve('http://openweathermap.org/data/2.1/find/name?q=urbandale','weatherfile.txt') 
When I execute the code I get this content in weatherfile.txt:
{"message":"","cod":"200","type":"base","calctime":"","units":"internal","count":1,"list":[{"id":4879890,"coord":{"lat":41.626659,"lon":-93.712173},"name":"Urbandale","main":{"temp":303.92,"pressure":1008,"humidity":52,"temp_min":303.15,"temp_max":304.26},"dt":1372190797,"date":"2013-06-25 20:06:37","wind":{"speed":5.7,"deg":200,"gust":9.3},"clouds":{"all":1},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01d"}],"sys":{"country":"US","population":39463},"url":"http:\/\/openweathermap.org\/city\/4879890"}]}

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Tue Jun 25, 2013 10:12 pm

thank you so much i have one more question
now since i have the right scrip im going to add that to another script i have (my first pyton script just asks you a bunch of questions and talks back to you simple stuff that a noob needs to know)

but what i want to know is after i run that command is there a way i can import that date from the file it wrights into the same script so it would looks something like this

Code: Select all

from urllib.request import urlretrieve
urlretrieve('http://www.myweather2.com/developer/forecast.ashx?uac=29bsTnYaZT&output=json&query=50323&temp_unit=f','weather')

#now i need a command here to open up the file and print its contents and then continue on to the next question in the same script

does that make since

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Tue Jun 25, 2013 10:45 pm

You should really spend a bit of time choosing what bits of the file are worth displaying, rather than just dumping the whole thing on the screen. It is not in a user-friendly format. It looks like the temperatures are given in degrees Kelvin, which you could change to fahrenheit, etc.

Anyway, if you just want to display the file as is:

Code: Select all

with open('weatherfile.txt','r') as f:
       print(f.read())
should work.

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: urllib2

Tue Jun 25, 2013 10:52 pm

how would i select witch things to display
i really do appreciate this help alot

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: urllib2

Wed Jun 26, 2013 7:19 am

You will only learn python by researching and writing code yourself. I'll get you started on a display and leave it to you to finish off. There is often more than one way to solve a problem in python, and there may be better ways than the one I will show you.
So you have a long STRING of data in weatherfile.txt which is in poor form to show anybody. Let's pick out a single piece of data to display instead, like the maximum temperature. This is somewhere in the middle of the string: ...p_min":303.15,"temp_max":304.26},"dt":137...
You will notice that the whole string has a number of sections separated by commas, and the commas can be used to turn the long string into a series of elements in a LIST using a python string method called split:

Code: Select all

>>> with open('weatherfile.txt','r') as f:
                (f.read()).split(',')
This generates a list, enclosed in square brackets, whose individual elements are pieces of the original string, just separated by commas:

['{"message":""', '"cod":"200"', '"type":"base"', '"calctime":""', '"units":"internal"', '"count":1', '"list":[{"id":4879890', '"coord":{"lat":41.626659', '"lon":-93.712173}', '"name":"Urbandale"', '"main":{"temp":303.92', '"pressure":1008', '"humidity":52', '"temp_min":303.15', '"temp_max":304.26}', '"dt":1372190797', '"date":"2013-06-25 20:06:37"', '"wind":{"speed":5.7', '"deg":200', '"gust":9.3}', '"clouds":{"all":1}', '"weather":[{"id":800', '"main":"Clear"', '"description":"Sky is Clear"', '"icon":"01d"}]', '"sys":{"country":"US"', '"population":39463}', '"url":"http:\\/\\/openweathermap.org\\/city\\/4879890"}]} \n']

(You could split the string using any character you want as a separator, like : or { but , is probably the best for this case).
So now you can see the list element '"temp_max":304.26}', which we need to access and then alter a bit.
There is another string method called startswith, which can be used to isolate the list element we want:

Code: Select all

>>> with open('weatherfile.txt','r') as f:
               for element in (f.read()).split(','):
                   if element.startswith('"temp_max'):
                        print(element)
This should produce the output "temp_max":304.26} which is getting close to what you need. But you don't really want to print this string. You want to assign it a name, strip away all the characters in before the first digit, remove the trailing } symbol, subtract 273, then print it in an appropriate line. These are steps you should try to do yourself.

Return to “Python”