Python Forum

Full Version: function to write the contents of a webpage into a new file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
"""
Create a function called wget, which takes a single argument -
a fully qualified URL (e.g. https://www.yahoo.com/).

The function must:
  1. Save the contents returned by that URL (the HTTP response body) into a
  file. The filename must be the same as the FQDN part of input URL. The file
  must be created in the current working directory (CWD).
  2. The value of the Content Type HTTP header must be returned as the function
    result, regardless of the actual HTTP code.
  3. The function documentation should read:
    Download the contents of an URL and return response's mimetype

HINT: Look into urlparse.urlparse() for FQDN retrieval.

For example:
  wget('https://yahoo.com/') -> 'text/html'
  # Creates/Rewrites a file called yahoo.com with the contents of yahoo.com


import urllib2
from urlparse import urlparse

def wget(*args):
    u = args
    obj = urlparse(url)
    fn = obj.netloc
    response = urllib2.urlopen(u)
    with open('fn','w') as f:
        f.write(response(read))
        f.close
url = 'http://yahoo.com/'
wget(url)
What am i doing wrong ...
I'm getting this error
Traceback (most recent call last):
  File "wget.py", line 24, in <module>
    import urllib2
  File "C:\Anaconda2\lib\urllib2.py", line 94, in <module>
    import httplib
  File "C:\Anaconda2\lib\httplib.py", line 72, in <module>
    import socket
  File "C:\Anaconda2\lib\socket.py", line 47, in <module>
    import _socket
ImportError: DLL load failed: %1 is not a valid Win32 application.
Looks like it could be some 32bit/64bit mismatch, what bit is your o/s and what bit are your installs.
What bit(32/64) of Anaconda2 and Windows do you use?
As mention bye Yoriz,there is a mix-up in 32-bit 64-bit.
Because urllib is part of standard library,there is no mix-up in modules.
Then the mix-up probably higher level between Anaconda/Windows.
What version of python are you using?  urllib2 was a 2.x thing, and was renamed in 3.x.

...which is stated as the very first line of the docs.  They're good and easy to understand, I promise!
https://docs.python.org/2/library/urllib...le-urllib2 Wrote:Note: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
(Dec-01-2016, 09:17 PM)nilamo Wrote: [ -> ]What version of python are you using?  urllib2 was a 2.x thing, and was renamed in 3.x.
That's not the problem here.
If use urllib2 in Python 3.x,
then this error would have been thrown before the error he gets.
>>> import urllib2
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
ImportError: No module named 'urllib2'