Skip to content Skip to sidebar Skip to footer

Write Contents Of URL Request To File

I am trying to fetch a list from a php file using python and save it to a file: import urllib.request page = urllib.request.urlopen('http://crypto-bot.hopto.org/server/list.php')

Solution 1:

Use urllib.urlretrieve (urllib.request.urlretrieve in Python 3).

In the console:

>>> import urllib
>>> urllib.urlretrieve('http://crypto-bot.hopto.org/server/list.php','test.txt') 
('test.txt', <httplib.HTTPMessage instance at 0x101338050>)

This results in a file, test.txt, being saving in the current working directory with the contents

ALF
AMC
ANC
ARG
...etc...

Solution 2:

You need to read from the file object before writing to the file. Also you should the same object to both file and screen.

Do this:

import urllib.request

page = urllib.request.urlopen('http://crypto-bot.hopto.org/server/list.php')

f = open("test.txt", "w")
content = page.read()
f.write(content)
f.close()

print(content)

Solution 3:

You're not reading the content from the urlopen file-like when you write to the file.

Also, shutil.copyfileobj().


Post a Comment for "Write Contents Of URL Request To File"