0
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.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by richieking
Featured Replies
  • 1

    I did not get same error, but you did not decode bytes. [CODE]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') [/CODE] Read More

1

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')
0

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.

0

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

Edited by richieking: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.