With regards to urllib AttributeError: 'module' object has no attribute 'urlopen'

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-12-21T07:11:38Z Indexed on 2010/12/21 7:54 UTC
Read the original article Hit count: 302

Filed under:
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()

© Stack Overflow or respective owner

Related posts about python