<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&quot;DE8CQn8_cCp7ImA9WxBSGE0.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188</id><updated>2009-12-25T21:54:23.148-08:00</updated><title>How To Examples for Python</title><subtitle type='html'>How to do interesting things in Python with line by line explanations</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default?redirect=false&amp;v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry gd:etag='W/&quot;DEIMQXw_fCp7ImA9WxBSGE0.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188.post-7248485939527094284</id><published>2009-12-25T21:44:00.000-08:00</published><updated>2009-12-25T21:49:40.244-08:00</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2009-12-25T21:49:40.244-08:00</app:edited><category scheme='http://www.blogger.com/atom/ns#' term='time python YYYYMMDD format today now'/><title>How to get today's date in YYYYMMDD format</title><content type='html'>&lt;b&gt;Code&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import time                           #1&lt;br /&gt;print time.strftime("%Y%m%d")         #2  &lt;br /&gt;print time.strftime("%Y%m%d%H%M%S")   #3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;How this works - Line by Line&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;import time                           #1&lt;/pre&gt;&lt;br /&gt;Imports the time module which is standard&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;print time.strftime("%Y%m%d")         #2&lt;/pre&gt;&lt;br /&gt;Prints the date in "YYYYMMDD" format.  Note the upper case Y and lower case m and d.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;print time.strftime("%Y%m%d%H%M%S")   #3&lt;/pre&gt;&lt;br /&gt;Prints the time in "YYYYMMDDHHMMSS" format.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8176887566863819188-7248485939527094284?l=howtoexamplesforpython.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/7248485939527094284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-get-todays-date-in-yyyymmdd.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/7248485939527094284?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/7248485939527094284?v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-get-todays-date-in-yyyymmdd.html' title='How to get today&apos;s date in YYYYMMDD format'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email><gd:extendedProperty name='OpenSocialUserId' value='07523560750866947102'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry gd:etag='W/&quot;CEQBRn05fip7ImA9WxBSGE0.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188.post-1234654494422040974</id><published>2009-12-25T20:10:00.000-08:00</published><updated>2009-12-25T20:39:17.326-08:00</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2009-12-25T20:39:17.326-08:00</app:edited><category scheme='http://www.blogger.com/atom/ns#' term='Python ClientForm BeautifulSoup Craigslist Response Search'/><title>How to fill out the search form on craigslist and display the results</title><content type='html'>&lt;b&gt;Code&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;from urllib2 import urlopen                           #1&lt;br /&gt;from ClientForm import ParseResponse                  #2&lt;br /&gt;from BeautifulSoup import BeautifulSoup               #3&lt;br /&gt;&lt;br /&gt;html = urlopen("http://sfbay.craigslist.org/cta")     #4&lt;br /&gt;forms = ParseResponse(html, backwards_compat = False) #5&lt;br /&gt;form = forms[0]                                       #6&lt;br /&gt;form['query'] = "toyota"                              #7  &lt;br /&gt;form['srchType'] = ["T"]                              #8&lt;br /&gt;&lt;br /&gt;response = urlopen(form.click())                      #9&lt;br /&gt;soup = BeautifulSoup(response)                        #10&lt;br /&gt;&lt;br /&gt;rows = soup('p')                                      #11&lt;br /&gt;&lt;br /&gt;for row in rows:                                      #12&lt;br /&gt;    print row('a')[0].contents[0]                     #13 &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;How this works - Line by Line&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;from urllib2 import urlopen                           #1&lt;/pre&gt;&lt;br /&gt;This imports the urlopen method from the standard urllib2 module&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;from ClientForm import ParseResponse                  #2&lt;/pre&gt;&lt;br /&gt;This imports the ParseResponse method from the ClientForm module which you can get &lt;a href="http://wwwsearch.sourceforge.net/ClientForm/#download"&gt;HERE&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;from BeautifulSoup import BeautifulSoup               #3&lt;/pre&gt;&lt;br /&gt;This imports the BeautifulSoup method from the BeautifulSoup module which you can get &lt;a href="http://www.crummy.com/software/BeautifulSoup/"&gt;HERE&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;html = urlopen("http://sfbay.craigslist.org/cta")     #4&lt;/pre&gt;&lt;br /&gt;This defines the variable &lt;span style="font-style:italic;"&gt;html&lt;/span&gt; as the html of the cars and trucks category of craigslist San Francisco Bay Area.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;forms = ParseResponse(html, backwards_compat = False) #5&lt;/pre&gt;&lt;br /&gt;This defines the variable &lt;span style="font-style:italic;"&gt;forms&lt;/span&gt; as the result set of the ParseResponse method which has the &lt;span style="font-style:italic;"&gt;html&lt;/span&gt; variable passed into it.  The result set is a list of forms on the website.  Since some websites have more than one form, a result set is returned from the ParseResponse method.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;form = forms[0]                                       #6&lt;/pre&gt;&lt;br /&gt;This defines the variable &lt;span style="font-style:italic;"&gt;form&lt;/span&gt; as the first form in the result set variable &lt;span style="font-style:italic;"&gt;forms&lt;/span&gt;.  In plain english, since there is only one form on the craigslist website, this sets the variable form to that one form. If there was more than one form, you'd have to figure out which form you are trying to fill out and set the variable form to the appropriate forms[x].  You can do this by doing the following:&lt;br /&gt;&lt;pre&gt;for form in forms:&lt;br /&gt;    print form&lt;br /&gt;    print&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;form['query'] = "toyota"                              #7&lt;/pre&gt;&lt;br /&gt;This fills out the query of the form to "toyota".  You can view the fields that are in the form by printing the form (as seen in the explanation for line #6).&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;form['srchType'] = ["T"]                              #8&lt;/pre&gt;&lt;br /&gt;This sets the field on the form to search only titles&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;response = urlopen(form.click())                      #9&lt;/pre&gt;&lt;br /&gt;This submits the query and sets the html response as the variable &lt;span style="font-style:italic;"&gt;response&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;soup = BeautifulSoup(response)                        #10&lt;br /&gt;rows = soup('p')                                      #11&lt;br /&gt;for row in rows:                                      #12&lt;br /&gt;    print row('a')[0].contents[0]                     #13&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;See my posting on "How to screen scrape craigslist using python and BeautifulSoup" for how this works.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8176887566863819188-1234654494422040974?l=howtoexamplesforpython.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/1234654494422040974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-fill-out-search-form-on.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/1234654494422040974?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/1234654494422040974?v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-fill-out-search-form-on.html' title='How to fill out the search form on craigslist and display the results'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email><gd:extendedProperty name='OpenSocialUserId' value='07523560750866947102'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry gd:etag='W/&quot;A0UHQHc9eSp7ImA9WxBSF0Q.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188.post-2512319997092438745</id><published>2009-12-25T19:35:00.000-08:00</published><updated>2009-12-25T19:47:11.961-08:00</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2009-12-25T19:47:11.961-08:00</app:edited><category scheme='http://www.blogger.com/atom/ns#' term='python ide idle redirect back sys.stdout'/><title>How to redirect sys.stdout from a file back to idle</title><content type='html'>&lt;b&gt;CODE&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys                          #1&lt;br /&gt;idlestdout = sys.stdout             #2&lt;br /&gt;outfile = open('outfile.txt','w')   #3&lt;br /&gt;sys.stdout = outfile                #4&lt;br /&gt;print 'hello world'                 #5&lt;br /&gt;outfile.flush()                     #6&lt;br /&gt;outfile.close()                     #7&lt;br /&gt;sys.stdout = idlestdout             #8&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt; How this works - Line by Line&lt;/b&gt;&lt;br /&gt;Note that this code redirects the standard output from a file back to any IDE, not just idle.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;import sys                          #1&lt;/pre&gt;&lt;br /&gt;This imports the sys module which comes standard&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;idlestdout = sys.stdout             #2&lt;/pre&gt;&lt;br /&gt;This defines the variable &lt;span style="font-style:italic;"&gt;idlestdout&lt;/span&gt; as the current output.  You need to do this so that Python later knows what to redirect its output back to.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;outfile = open('outfile.txt','w')   #3&lt;/pre&gt;&lt;br /&gt;This defines the variable &lt;span style="font-style:italic;"&gt;outfile&lt;/span&gt; as a file called "outfile.txt" that we will be writing to.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sys.stdout = outfile                #4&lt;/pre&gt;&lt;br /&gt;This redirects the standard output to the file defined in line #3&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;print 'hello world'                 #5&lt;/pre&gt;&lt;br /&gt;This prints "hello world" to the standard output memory, or in this case, the &lt;span style="font-style:italic;"&gt;outfile&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;outfile.flush()                     #6&lt;/pre&gt;&lt;br /&gt;This writes all of the pending text (line #5) to the outfile&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;outfile.close()                     #7&lt;/pre&gt;&lt;br /&gt;This closes the outfile file&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sys.stdout = idlestdout             #8&lt;/pre&gt;&lt;br /&gt;This redirects the sys.stdout back to the IDE.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8176887566863819188-2512319997092438745?l=howtoexamplesforpython.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/2512319997092438745/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-redirect-sysstdout-from-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/2512319997092438745?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/2512319997092438745?v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-redirect-sysstdout-from-file.html' title='How to redirect sys.stdout from a file back to idle'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email><gd:extendedProperty name='OpenSocialUserId' value='07523560750866947102'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry gd:etag='W/&quot;AkQMQHozeip7ImA9WxBSF0Q.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188.post-333144732916815011</id><published>2009-12-25T19:10:00.000-08:00</published><updated>2009-12-25T19:33:01.482-08:00</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2009-12-25T19:33:01.482-08:00</app:edited><category scheme='http://www.blogger.com/atom/ns#' term='python yahoo finance stock quotes ystockquote'/><title>How to get stock quotes and other data from yahoo finance</title><content type='html'>&lt;b&gt;Code&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import ystockquote                                     #1&lt;br /&gt;for item in ystockquote.get_all('goog').iteritems():   #2&lt;br /&gt;    print item                                         #3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;How this works - Line by Line&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;import ystockquote                                     #1&lt;/pre&gt;&lt;br /&gt;This imports the ystockquote module which you can get &lt;a href="http://www.goldb.org/ystockquote.html"&gt;HERE&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for item in ystockquote.get_all('goog').iteritems():   #2&lt;/pre&gt;&lt;br /&gt;You can change the stock ticker from 'goog' to the ticker of interest.&lt;br /&gt;for example, to get yahoo's stock quote, change 'goog' to 'yhoo'&lt;br /&gt;This loops through each item in the ystockquote fields&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;    print item                                         #3&lt;/pre&gt;&lt;br /&gt;This prints each item&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8176887566863819188-333144732916815011?l=howtoexamplesforpython.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/333144732916815011/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-get-stock-quotes-and-other-data.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/333144732916815011?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/333144732916815011?v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-get-stock-quotes-and-other-data.html' title='How to get stock quotes and other data from yahoo finance'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email><gd:extendedProperty name='OpenSocialUserId' value='07523560750866947102'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry gd:etag='W/&quot;DUEBQnc4fCp7ImA9WxBSF0Q.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188.post-3864040175546217132</id><published>2009-12-25T18:53:00.000-08:00</published><updated>2009-12-25T19:20:53.934-08:00</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2009-12-25T19:20:53.934-08:00</app:edited><category scheme='http://www.blogger.com/atom/ns#' term='python loop last object item'/><title>How to get the last item of a loop</title><content type='html'>&lt;b&gt;Code&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;items = ["hello","world"]                   #1&lt;br /&gt;for item in items:                          #2&lt;br /&gt;    if item == items[-1]:                   #3&lt;br /&gt;        print "%s is the last item" %item   #4&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;How this works - line by line&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;items = ["hello","world"]                   #1&lt;/pre&gt;&lt;br /&gt;I'm setting a list called items for demonstration purposes.&lt;br /&gt;The contents of this list are two strings - "hello" and "world"&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for item in items:                          #2&lt;/pre&gt;&lt;br /&gt;looping through the two strings in the list&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;    if item == items[-1]:                   #3&lt;/pre&gt;&lt;br /&gt;if the current item in the loop is the last item in the list..&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;        print "%s is the last item" %item   #4&lt;/pre&gt;&lt;br /&gt;then print "%s is the last item"&lt;br /&gt;&lt;br /&gt;Note that if you wanted the first object in the loop, you'd change line #3 to &lt;br /&gt;&lt;pre&gt;    if item == items[0]:                  &lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8176887566863819188-3864040175546217132?l=howtoexamplesforpython.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/3864040175546217132/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-get-last-item-of-loop.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/3864040175546217132?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/3864040175546217132?v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-get-last-item-of-loop.html' title='How to get the last item of a loop'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email><gd:extendedProperty name='OpenSocialUserId' value='07523560750866947102'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry gd:etag='W/&quot;DUENRn8_fSp7ImA9WxBSF0Q.&quot;'><id>tag:blogger.com,1999:blog-8176887566863819188.post-2016699410639361712</id><published>2009-12-25T17:50:00.000-08:00</published><updated>2009-12-25T19:21:37.145-08:00</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2009-12-25T19:21:37.145-08:00</app:edited><category scheme='http://www.blogger.com/atom/ns#' term='craigslist python beautifulsoup screen scrape'/><title>How to screen scrape craigslist using Python and BeautifulSoup</title><content type='html'>&lt;b&gt;Code&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;from BeautifulSoup import BeautifulSoup   #1&lt;br /&gt;from urllib2 import urlopen               #2&lt;br /&gt;&lt;br /&gt;site = "http://sfbay.craigslist.org/rea/" #3&lt;br /&gt;html = urlopen(site)                      #4&lt;br /&gt;soup = BeautifulSoup(html)                #5&lt;br /&gt;postings = soup('p')                      #6&lt;br /&gt;&lt;br /&gt;for post in postings:                     #7&lt;br /&gt;    print post('a')[0].contents[0]        #8&lt;br /&gt;    print post('a')[0]['href']            #9&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;How this works - line by line:&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;from BeautifulSoup import BeautifulSoup   #1&lt;/pre&gt;&lt;br /&gt;this imports the BeautifulSoup method from the BeautifulSoup module&lt;br /&gt;you can get this module from &lt;a href="http://www.crummy.com/software/BeautifulSoup/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;from urllib2 import urlopen               #2&lt;/pre&gt;&lt;br /&gt;this imports the urlopen method from the urllib2 module &lt;br /&gt;this module comes standard with the most recent versions of python&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;site = "http://sfbay.craigslist.org/rea/" #3&lt;/pre&gt;&lt;br /&gt;this defines the variable &lt;span style="font-style:italic;"&gt;site&lt;/span&gt; as a craigslist site.&lt;br /&gt;In this case, the san francisco bay area real estate.&lt;br /&gt;you can change the site value to match the site you want to scrape&lt;br /&gt;for example, san francisco bay area cars+trucks would be&lt;br /&gt;"http://sfbay.craigslist.org/cta/"&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;html = urlopen(site)                      #4&lt;/pre&gt;&lt;br /&gt;this defines the variable &lt;span style="font-style:italic;"&gt;html&lt;/span&gt; as the html source code of the &lt;span style="font-style:italic;"&gt;site&lt;/span&gt; variable&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;soup = BeautifulSoup(html)                #5&lt;/pre&gt;&lt;br /&gt;this defines the variable &lt;span style="font-style:italic;"&gt;soup&lt;/span&gt; as html that has been parsed by BeautifulSoup&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;postings = soup('p')                      #6&lt;/pre&gt;&lt;br /&gt;this defines the variable &lt;span style="font-style:italic;"&gt;postings&lt;/span&gt; as the 'p' tags in the variable soup.  &lt;span style="font-style:italic;"&gt;postings&lt;/span&gt; is a resultset, i.e. a set of objects&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for post in postings:                     #7&lt;/pre&gt;&lt;br /&gt;since the postings is a resultset, we need to loop through the set to get the individual post header and link&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;    print post('a')[0].contents[0]        #8&lt;/pre&gt;&lt;br /&gt;this returns the header of the post.  this works as follows -- the header is the contents of the first 'a' tag within each 'p' tag (i.e. the postings).  &lt;br /&gt;&lt;br /&gt;Thus, post('a') returns a resultset of 'a' items in 'p' and post('a')[0] returns the first 'a' item in the set.  &lt;br /&gt;&lt;br /&gt;To understand this more clearly, i've provided the following example:  If the html looks like this:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;&amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;a&amp;gt;hello&amp;lt;/a&amp;gt;&lt;br /&gt;    &amp;lt;a&amp;gt;world&amp;lt;a/&amp;gt;&lt;br /&gt;&amp;lt;/p&amp;gt;&lt;br /&gt;&amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;a&amp;gt;my&amp;lt;/a&amp;gt;&lt;br /&gt;    &amp;lt;a&amp;gt;name&amp;lt;/a&amp;gt;&lt;br /&gt;    &amp;lt;a&amp;gt;is&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;/p&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;then after this has been passed through soup = BeautifulSoup(html),&lt;br /&gt;soup('p')[0]('a')[0] = hello&lt;br /&gt;soup('p')[0]('a')[1] = world&lt;br /&gt;soup('p')[1]('a')[0] = my&lt;br /&gt;soup('p')[1]('a')[1] = name&lt;br /&gt;soup('p')[1]('a')[2] = is&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;    print post('a')[0]['href']            #9&lt;/pre&gt;&lt;br /&gt;to get the link, you need to get the ['href'] of each post.  Note that the link provided needs to be appended to the appropriate prefix link&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8176887566863819188-2016699410639361712?l=howtoexamplesforpython.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://howtoexamplesforpython.blogspot.com/feeds/2016699410639361712/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-screen-scrape-craigslist-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/2016699410639361712?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8176887566863819188/posts/default/2016699410639361712?v=2'/><link rel='alternate' type='text/html' href='http://howtoexamplesforpython.blogspot.com/2009/12/how-to-screen-scrape-craigslist-using.html' title='How to screen scrape craigslist using Python and BeautifulSoup'/><author><name>Jay</name><uri>http://www.blogger.com/profile/03669510513728840462</uri><email>noreply@blogger.com</email><gd:extendedProperty name='OpenSocialUserId' value='07523560750866947102'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry></feed>