Python: Beautiful Soup

Beautiful Soup

“A tremendous boon.” — Python411 Podcast

[ Download | Documentation | Hall of Fame | Source | Discussion group ]

If Beautiful Soup has saved you a lot of time and money, the best way to pay me back is to check out Constellation Games, my sci-fi novel about alien video games.
You can read the first two chapters for free, and the full novel starts at 5 USD. Thanks!

If you have questions, send them to the discussion group. If you find a bug, file it.

Beautiful Soup is a Python library designed for quick turnaround projects like screen-scraping. Three features make it powerful:

  1. Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. It doesn’t take much code to write an application
  2. Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don’t have to think about encodings, unless the document doesn’t specify an encoding and Beautiful Soup can’t detect one. Then you just have to specify the original encoding.
  3. Beautiful Soup sits on top of popular Python parsers like lxml and html5lib, allowing you to try out different parsing strategies or trade speed for flexibility.

Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. You can tell it “Find all the links”, or “Find all the links of class externalLink“, or “Find all the links whose urls match “foo.com”, or “Find the table heading that’s got bold text, then give me that text.”

Valuable data that was once locked up in poorly-designed websites is now within your reach. Projects that would have taken hours take only minutes with Beautiful Soup.

Interested? Read more.

 

SOURCEhttp://www.crummy.com/software/BeautifulSoup/

Python: socket basics

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 mysock.connect(('www.py4inf.com', 80))
 mysock.send('GET http://www.pythonlearn.com/code/intro-short.txt HTTP/1.0\n\n')
while True:
 data = mysock.recv(512)
 if ( len(data) < 1 ) :
 break
 print data;
mysock.close()

Python String strip() Method

Description

The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

Syntax

Following is the syntax for strip() method −

str.strip([chars]);

Parameters

  • chars — The characters to be removed from beginning or end of the string.

Return Value

This method returns a copy of the string in which all chars have been stripped from the beginning and the end of the string.

Example

The following example shows the usage of strip() method.

#!/usr/bin/python

str = "0000000this is string example....wow!!!0000000";
print str.strip( '0' )

When we run above program, it produces following result −

this is string example....wow!!!


SOURCE: http://www.tutorialspoint.com/python/string_strip.htm

Python: ranges and Lambda

evens_to_50 = [i for i in range(51) if i % 2 == 0]print evens_to_5
dobles_por_3 = [x*2 for x in range(1,6) if (x*2) % 3 == 0]
# Completa la siguiente línea. Usá la línea de arriba como ayuda.
cuadrados_pares = [x*2 for x in range(1,12) if (x*2) % 2 == 0] 
print cuadrados_pares

### LAMBDA:
mi_lista = range(16)
print filter(lambda x: x % 3 == 0, mi_lista)

lenguajes = ["HTML", "JavaScript", "Python", "Ruby"]
print filter(lambda i: i == "Python" , lenguajes)

incomprensible = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
mensaje = filter(lambda x: x!='X', incomprensible[::-1])print mensaje