Python Forum
function to write the contents of a webpage into a new file
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function to write the contents of a webpage into a new file
#1
"""
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.
Reply
#2
Looks like it could be some 32bit/64bit mismatch, what bit is your o/s and what bit are your installs.
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to Write a function with Pandas? SuperNinja3I3 4 2,317 Jul-03-2022, 02:19 PM
Last Post: ndc85430
  How can I write a function with three parameters? MehmetAliKarabulut 3 2,764 Mar-04-2021, 05:23 PM
Last Post: buran
  about write file wrong (Edit directly online) CNenfan 4 2,497 Jan-29-2021, 05:32 AM
Last Post: deanhystad
  Write to file emilng 1 1,703 Nov-08-2020, 08:44 PM
Last Post: Gribouillis
  Write function to find decryptionkey kotter 3 3,100 Dec-11-2019, 07:04 PM
Last Post: nilamo
  help! function that checks if a file is a bed file, tips so i can code it myself lilyS 1 2,358 May-31-2019, 12:41 PM
Last Post: ichabod801
  write split words of sentence to file bluefrog 1 2,975 Aug-27-2018, 01:28 AM
Last Post: micseydel
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,026 Jan-11-2018, 07:24 AM
Last Post: wavic
  how to write a function in python that solves an equation samiraheen 5 9,755 Sep-28-2017, 01:10 AM
Last Post: Mekire
  Write a function sorting(L)? MoIbrahim 11 8,273 Apr-22-2017, 11:45 AM
Last Post: idontreallywolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020