0
import re
import string
import shutil
import os
import os.path
import time
import datetime
import math
import urllib
from array import array
import random

filehandle = urllib.urlopen('http://www.google.com/') #open webpage
s = filehandle.read() #read 
print s #display

#what i plan to do with it once i get the first part working
#results = re.findall('[<td style="font-weight:bold;" nowrap>$][0-9][0-9][0-9][.][0-9][0-9][</td></tr></tfoot></table>]',s)
#earnings = '$ '
#for money in results:
#earnings = earnings + money[1]+money[2]+money[3]+'.'+money[5]+money[6]
#print earnings
#raw_input()

this is the code that i have so far. now i have looked at all the other forums that give solutions such as the name of the script, which is parse_Money.py, and i have tried doing it with urllib.request.urlopen AND i have tried running it on python 2.5, 2.6, and 2.7. If anybody has any suggestions it would be really welcome, thanks everyone!! --Matt

---EDIT--- I also tried this code and it worked, so im thinking its some kind of syntax error, so if anybody with a sharp eye can point it out, i would be very appreciative.

import shutil
import os
import os.path
import time
import datetime
import math
import urllib
from array import array
import random
b = 3

#find URL
URL = raw_input('Type the URL you would like to read from[Example: http://www.google.com/] :')



while b == 3:
    #get file name
    file1 = raw_input('Enter a file name for the downloaded code:')
    filepath = file1 + '.txt'
    if os.path.isfile(filepath):
        print 'File already exists'
        b = 3
    else:
        print 'Filename accepted'
        b = 4

file_path = filepath
#open file
FileWrite = open(file_path, 'a')


#acces URL
filehandle = urllib.urlopen(URL)

#display souce code
for lines in filehandle.readlines():
    FileWrite.write(lines)
    print lines
print 'The above has been saved in both a text and html file'

#close files
filehandle.close()
FileWrite.close()
2
  • 1
    Looks like you totally screwed up your Python installation. What's the operating system and what is inside the file with the filename urllib.__file__? Commented Dec 21, 2010 at 7:17
  • i've tried running your erroneous code on IDLE, and it executed just fine. though i didn't import anything besides urllib. probably you might want to try doing that. other than that, i don't think there's anything wrong w/ your code in the first example. Commented Dec 21, 2010 at 8:45

3 Answers 3

1

it appears that the urlopen method is available in the urllib.request module and not in the urllib module as you're expecting.

rule of thumb - if you're getting an AttributeError, that field/operation is not present in the particular module.

EDIT - Thanks to AndiDog for pointing out - this is a solution valid for Py 3.x, and not applicable to Py2.x!

Sign up to request clarification or add additional context in comments.

3 Comments

the thing is if i import urllib.request or if i call urllib.request.urlopen it says the same thing. and what really puzzles me is that i had it running earlier today on these commands that you see above
He said he's using Python 2.x, not 3.x! They moved urlopen into urllib.request in Python 3.0 and newer.
my bad! edited the response to indicate AndiDog's observation. And in addition to his suggestion of checking the file location, you can also invoke urllib.__dict__.get("urlopen") to check whether the module actually even contains that operation.
0

The urlopen function is actually in the urllib2 module. Try import urllib2 and use urllib2.urlopen

Comments

0

I see that you are using Python2 or at least intend to use Python2. urlopen helper function is available in both urllib and urllib2 in Python2.

What you need to do this, execute this script against the correct version of your python

C:\Python26\python.exe yourscript.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.