Download a web page

From IronPython Cookbook

(Difference between revisions)
m (Reverted edits by Anjila428 (Talk) to last revision by MichaelFoord)
 
Line 1: Line 1:
-
Hi, I am Anjila Wilson and.I am resident of United States. I love music and net surfing. I got some health related
+
== Using the WebClient class ==
-
informations from some articles.You can see from this link
+
A simple example:
 +
<pre>
 +
from System.Net import WebClient
 +
content = WebClient().DownloadString("http://google.com")
 +
print content
 +
</pre>
 +
There are other useful shortcuts as well. For example, to download a 'page' (or resource) from the internet and save it as a file, you can use the [http://msdn2.microsoft.com/en-us/library/system.net.webclient.downloadfile.aspx WebClient.DownloadFile] method:
 +
<pre>
 +
from System.Net import WebClient
 +
WebClient().DownloadFile(url, filename)
 +
</pre>
-
[http://www.acaiberryinformations.com acai berry supplement]
+
 
-
[http://www.articlesbase.com/supplements-and-vitamins-articles/my-weight-loss-experience-with-the-acai-berry-diet-1726693.html Acai Berry]
+
The longer way, that is more flexible if you want to configure the [http://msdn2.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx WebClient] class, is:
-
[http://ezinearticles.com/?Acai-Berry---How-I-Lost-30-Pounds-in-Under-30-Days-Using-The-Acai-Berry&id=1998407 Acai Berry]
+
 
-
[http://www.buzzle.com/articles/the-facts-and-half-truths-about-the-acai-berry-diet.html Acai Berry]
+
<pre>
 +
from System.Net import WebClient
 +
from System.IO import StreamReader
 +
 
 +
client = WebClient()
 +
dataStream = client.OpenRead('http://google.com')
 +
reader = StreamReader(dataStream)
 +
result = reader.ReadToEnd()
 +
print result
 +
</pre>
 +
 
 +
== Using the WebRequest and WebResponse classes ==
 +
<pre>
 +
from System.Net import WebRequest
 +
request = WebRequest.Create("http://google.com")
 +
response = request.GetResponse()
 +
responseStream = response.GetResponseStream()
 +
from System.IO import StreamReader
 +
result = StreamReader(responseStream).ReadToEnd()
 +
print result
 +
</pre>
 +
 
 +
For a convenience function, which works whether you are making a 'POST' or a 'GET', see [[Submit a POST form and download the result web page]].
 +
 
 +
 
 +
Back to [[Contents]].

Current revision as of 16:17, 17 October 2010

Using the WebClient class

A simple example:

from System.Net import WebClient
content = WebClient().DownloadString("http://google.com")
print content

There are other useful shortcuts as well. For example, to download a 'page' (or resource) from the internet and save it as a file, you can use the WebClient.DownloadFile method:

from System.Net import WebClient
WebClient().DownloadFile(url, filename)


The longer way, that is more flexible if you want to configure the WebClient class, is:

from System.Net import WebClient
from System.IO import StreamReader

client = WebClient()
dataStream = client.OpenRead('http://google.com')
reader = StreamReader(dataStream)
result = reader.ReadToEnd()
print result

Using the WebRequest and WebResponse classes

from System.Net import WebRequest
request = WebRequest.Create("http://google.com")
response = request.GetResponse()
responseStream = response.GetResponseStream()
from System.IO import StreamReader
result = StreamReader(responseStream).ReadToEnd()
print result

For a convenience function, which works whether you are making a 'POST' or a 'GET', see Submit a POST form and download the result web page.


Back to Contents.

TOOLBOX
LANGUAGES