View Single Post
Old May 19th, 2006, 1:20 PM   #55
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
In the get_industry_urls function, there's this line:
links = soup.fetch("table")[7].fetch("a")
This line fetches all the links in table 7. Notice the [7]. This says you want only one item from the list.

A list contains multiple items. It can contain two, three, four or millions of items. But it can also contain just one, or no items whatsoever. It's still a list, regardless of how many items it has. In order to pull a single item from a list, you need to specify it's index.

For instance, if you have a list with a single letter in it:
list = ["a"]
To get the letter out of the list, you have to specify it's index. Because the list is only one item long, there is only one valid index: 0:
letter = list[0]
The same thing applies to your links. In order to get the first (and only) element out of a list of links:
single_link = soup.fetch("table")[7].fetch("a")[0]
BeautifulSoup provides a shortcut for this type of command, though. The following code is equivalent to the above code:
single_link = soup.fetch("table")[7].a
Arevos is offline   Reply With Quote