This example covers some basic functionality of urllib2 - opening a website, reading from it and finding its title.<br><br><br>diff --git a/data/GSOC examples/Opening websites b/data/GSOC examples/Opening we<br>
new file mode 100644<br>
index 0000000..97ee16f<br>
--- /dev/null<br>
+++ b/data/GSOC examples/Opening websites <br>
@@ -0,0 +1,64 @@<br>
+# This example demonstrates how urllib2 can be used to open websites and read<br>
+# some data from them.<br>
+<br>
+import urllib2<br>
+<br>
+# define a function which will open a bunch of links we give it in a list<br>
+def open_sites(links):<br>
+ sites = []<br>
+ for url in urls:<br>
+ print "Opening: " + url<br>
+ # try to open that site<br>
+ try:<br>
+ site = urllib2.urlopen(url)<br>
+ except:<br>
+ # Does an error occur with any of the default urls? <br>
+ # Practice: If so, could you fix it?<br>
+ print "An error has occured, skipping " + url<br>
+ print<br>
+ raw_input("...press enter key to continue...")<br>
+ continue<br>
+ if site.geturl() != url:<br>
+ print "Careful! Site " + url + " has redirected you to " + site.get<br>
+ print "Site " + site.geturl() + " is now open."<br>
+ print<br>
+ sites.append(site)<br>
+ raw_input("...press enter key to continue...")<br>
+ print<br>
+ return sites<br>
+<br>
+url1 = "<a href="http://www.google.com">http://www.google.com</a>"<br>
+url2 = "<a href="http://www.sugarlabs.org">http://www.sugarlabs.org</a>"<br>
+url3 = "<a href="http://www.wikipedia.org">www.wikipedia.org</a>"<br>
+urls = [url1, url2, url3]<br>
+<br>
+sites = open_sites(urls)<br>
+<br>
+print<br>
+print "Let's read those sites and find their titles."<br>
+print<br>
+raw_input("...press enter key to continue...")<br>
+print<br>
+<br>
+for site in sites:<br>
+ site_content = site.read()<br>
+ title_at = site_content.find("<title>") + 7<br>
+ print "The title of site at " + site.geturl() + " begins at its index " + s<br>
+ title_ends = site_content.find("</title>", title_at)<br>
+ title = site_content[title_at:title_ends]<br>
+ # In Python, \ is the so-called "escape" character. Since some characters h<br>
+ # special meanings, like " or ' opening and closing a string, we have to te<br>
+ # the interpreter to ignore such meanings when we wish to put those precise<br>
+ # characters in a string (or print them). In the following line, we wish to<br>
+ # print the " character so we "escape" it - by putting \ in before it.<br>
+ # Practice: What would we have to do to print an escape character \ ? <br>
+ print "The title is: \"" + title + "\""<br>
+ print<br>
+ # An index of -1 refers to the first element from the end. Thus, this <br>
+ # comparison checks whether the current element is the last one.<br>
+ # Practice: Why would we want that?<br>
+ if site == sites[-1]:<br>
+ raw_input("...press enter to finish..:")<br>
+ else:<br>
+ raw_input("...press enter key to continue...")<br>
+ print<br>
<br>