You gotta love a 2-line python solution

DFS nospam at dfs.com
Mon May 2 01:23:48 EDT 2016


On 5/2/2016 1:02 AM, Stephen Hansen wrote:
> On Sun, May 1, 2016, at 09:51 PM, DFS wrote:
>> On 5/2/2016 12:31 AM, Stephen Hansen wrote:
>>> On Sun, May 1, 2016, at 08:39 PM, DFS wrote:
>>>> To save a webpage to a file:
>>>> -------------------------------------
>>>> 1. import urllib
>>>> 2. urllib.urlretrieve("http://econpy.pythonanywhere.com
>>>>      /ex/001.html","D:\file.html")
>>>> -------------------------------------
>>>
>>> Note, for paths on windows you really want to use a rawstring. Ie,
>>> r"D:\file.html".
>>>
>> Thanks.
>>
>> I actually use "D:\\file.html" in my code.
>
> Or you can do that. But the whole point of raw strings is not having to
> escape slashes :)


Trying the rawstring thing (say it fast 3x):

webpage = "http://econpy.pythonanywhere.com/ex/001.html"

----------------------------------------------------
webfile = "D:\\econpy001.html"
urllib.urlretrieve(webpage,webfile)     WORKS
----------------------------------------------------
webfile = "rD:\econpy001.html"
urllib.urlretrieve(webpage,webfile)     FAILS
----------------------------------------------------
webfile = "D:\econpy001.html"
urllib.urlretrieve(webpage,"r" + webfile)     FAILS
----------------------------------------------------
webfile  = "D:\econpy001.html"
urllib.urlretrieve(webpage,"r" + "" + webfile + "")  FAILS
----------------------------------------------------

The FAILs throw:

Traceback (most recent call last):
   File "webscraper.py", line 54, in <module>
     urllib.urlretrieve(webpage,webfile)
   File "D:\development\python\python_2.7.11\lib\urllib.py", line 98, in 
urlretrieve
     return opener.retrieve(url, filename, reporthook, data)
   File "D:\development\python\python_2.7.11\lib\urllib.py", line 249, 
in retrieve
     tfp = open(filename, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'rD:\\econpy001.html'
----------------------------------------------------

What am I doing wrong?



More information about the Python-list mailing list