import urllib.request
import sys
#'http://www.imdb.com/list/SuSZdwCyHzU/'
def savePage(urll,filename):
    with urllib.request.urlopen(urll)as url:
        page=url.read()
    #    print(page)
    sys.argv[0]=filename
    outfile=open(sys.argv[0],'w')
    for line in page:
        outfile.write(line)
    outfile.close()

savePage('http://www.imdb.com/list/SuSZdwCyHzU/','outIMDB.txt')

so here is my code .I try to write the original codes into the 'outIMDB.text' but somehow I got this bug:
File "C:\Program Files (x86)\Wing IDE 101 4.0\src\debug\tserver\_sandbox.py", line 14, in <module>
File "C:\Program Files (x86)\Wing IDE 101 4.0\src\debug\tserver\_sandbox.py", line 11, in savePage
builtins.TypeError: must be str, not int
I appreciate it if someone can shed some light on that.

Recommended Answers

All 3 Replies

I did not get same error, but you did not decode bytes.

import urllib.request
import sys
import webbrowser
#'http://www.imdb.com/list/SuSZdwCyHzU/'
def savePage(urll,filename):
    page=urllib.request.urlopen(urll).read()
    print(page)
    sys.argv[0]=filename
    with open(sys.argv[0],'w') as outfile:
        outfile.write(page.decode('utf8'))

savePage('http://www.daniweb.com/software-development/python/threads/406620/1735920#post1735920','outIMDB.html')
webbrowser.open('outIMDB.html')

I did not get same error, but you did not decode bytes.

import urllib.request
import sys
import webbrowser
#'http://www.imdb.com/list/SuSZdwCyHzU/'
def savePage(urll,filename):
    page=urllib.request.urlopen(urll).read()
    print(page)
    sys.argv[0]=filename
    with open(sys.argv[0],'w') as outfile:
        outfile.write(page.decode('utf8'))

savePage('http://www.daniweb.com/software-development/python/threads/406620/1735920#post1735920','outIMDB.html')
webbrowser.open('outIMDB.html')

Thank you. it's about the decode thing.

And was there anythin special to include sys???
At least if you wanted a nice grib on the file operation. include os.

also you could simply use ....

with open(filename,"w") as opfile:
   opfile.write(foo.decode('utf8')

or use

with open(os.chmod(os.path.realpath(filename),0777),'w'):

to get super control over the created file.

in the end....

import urllib
import webbrowser
import os
#'http://www.imdb.com/list/SuSZdwCyHzU/'
def savePage(urll,filename):
    page=urllib.urlopen(urll).read()
    print(page)
    ##sys.argv[0]=filename
    with open(os.chmod(os.path.realpath(filename),0777),'w') as outfile:
        outfile.write(page.decode('utf8'))

savePage('http://www.daniweb.com/software-development/python/threads/406620/1735920#post1735920','outIMDB.html')
webbrowser.open('outIMDB
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.