Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108002

Re: You gotta love a 2-line python solution

From BartC <bc@freeuk.com>
Newsgroups comp.lang.python
Subject Re: You gotta love a 2-line python solution
Date 2016-05-02 10:26 +0100
Organization A noiseless patient Spider
Message-ID <ng769a$lp4$1@dont-email.me> (permalink)
References <ng6hur$tiu$1@dont-email.me>

Show all headers | View raw


On 02/05/2016 04:39, 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")
> -------------------------------------
>
> That's it!
>
> Coming from VB/A background, some of the stuff you can do with python -
> with ease - is amazing.
>
>
> VBScript version
> ------------------------------------------------------
> 1. Option Explicit
> 2. Dim xmlHTTP, fso, fOut
> 3. Set xmlHTTP = CreateObject("MSXML2.serverXMLHTTP")
> 4. xmlHTTP.Open "GET", "http://econpy.pythonanywhere.com/ex/001.html"
> 5. xmlHTTP.Send
> 6. Set fso = CreateObject("Scripting.FileSystemObject")
> 7. Set fOut = fso.CreateTextFile("D:\file.html", True)
> 8.  fOut.WriteLine xmlHTTP.ResponseText
> 9. fOut.Close
> 10. Set fOut = Nothing
> 11. Set fso  = Nothing
> 12. Set xmlHTTP = Nothing
> ------------------------------------------------------
>
> Technically, that VBS will run with just lines 3-9, but that's still 6
> lines of code vs 2 for python.

It seems Python provides a higher level solution compared with VBS. 
Python presumably also has to do those Opens and Sends, but they are 
hidden away inside urllib.urlretrieve.

You can do the same with VB just by wrapping up these lines in a 
subroutine. As you would if this had to be executed in a dozen different 
places for example. Then you could just write:

getfile("http://econpy.pythonanywhere.com/ex/001.html", "D:/file.html")

in VBS too. (The forward slash in the file name ought to work.)

(I don't know VBS; I assume it does /have/ subroutines? What I haven't 
factored in here is error handling which might yet require more coding 
in VBS compared with Python)

-- 
Bartc

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-01 23:39 -0400
  Re: You gotta love a 2-line python solution Stephen Hansen <me+python@ixokai.io> - 2016-05-01 21:31 -0700
    Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 00:51 -0400
      Re: You gotta love a 2-line python solution Stephen Hansen <me+python@ixokai.io> - 2016-05-01 22:02 -0700
        Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 01:08 -0400
          Re: You gotta love a 2-line python solution Stephen Hansen <me+python@ixokai.io> - 2016-05-01 22:21 -0700
            Re: You gotta love a 2-line python solution Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-02 15:51 +1000
        Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 01:23 -0400
          Re: You gotta love a 2-line python solution Stephen Hansen <me+python@ixokai.io> - 2016-05-01 22:37 -0700
            Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 02:13 -0400
  Re: You gotta love a 2-line python solution Terry Reedy <tjreedy@udel.edu> - 2016-05-02 02:46 -0400
  Re: You gotta love a 2-line python solution BartC <bc@freeuk.com> - 2016-05-02 10:26 +0100
    Re: You gotta love a 2-line python solution Marko Rauhamaa <marko@pacujo.net> - 2016-05-02 13:12 +0300
      Re: You gotta love a 2-line python solution Steven D'Aprano <steve@pearwood.info> - 2016-05-02 22:05 +1000
    Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 11:15 -0400
      Re: You gotta love a 2-line python solution Larry Martell <larry.martell@gmail.com> - 2016-05-02 11:24 -0400
      Re: You gotta love a 2-line python solution Manolo Martínez <manolo@austrohungaro.com> - 2016-05-02 17:32 +0200
  Re: You gotta love a 2-line python solution jfong@ms4.hinet.net - 2016-05-02 17:45 -0700
    Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 21:12 -0400
      Re: You gotta love a 2-line python solution jfong@ms4.hinet.net - 2016-05-02 20:27 -0700
        Re: You gotta love a 2-line python solution Stephen Hansen <me+python@ixokai.io> - 2016-05-02 20:49 -0700
          Re: You gotta love a 2-line python solution jfong@ms4.hinet.net - 2016-05-02 20:57 -0700
            Re: You gotta love a 2-line python solution Stephen Hansen <me+python@ixokai.io> - 2016-05-03 09:09 -0700
        Re: You gotta love a 2-line python solution DFS <nospam@dfs.com> - 2016-05-02 23:56 -0400
          Re: You gotta love a 2-line python solution Steven D'Aprano <steve@pearwood.info> - 2016-05-04 11:20 +1000
        Re: You gotta love a 2-line python solution Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-03 08:14 -0400

csiph-web