Re: Have list, need to scrub it

20 views
Skip to first unread message

Aaron DeVore

unread,
May 18, 2013, 4:39:55 PM5/18/13
to beauti...@googlegroups.com
Here is a shortcut to use for each element in the result of findAll:

    path = gameLink.a["href"]

In other words:

    a_tag = gameLink.find("a")  # Get first <a>
    path = gameLink["href"]  # Get gameLink's href attribute

Have fun!
-Aaron DeVore


On Sat, May 18, 2013 at 12:09 PM, Benjamin Lindqvist <benjamin...@gmail.com> wrote:
First time poster, utter beginner at BS, Python and programming in general, really. Trying to learn as I go along, so apologies if this is a really obvious question.

I've managed to write a function that fetches a webpage and turns the stuff I'm interested in into elements in a list.

from bs4 import BeautifulSoup

from urllib2 import urlopen


def get_category_links(section_url):

html = urlopen(section_url).read()

soup = BeautifulSoup(html)

gameLinks = soup.findAll("td","name")

return gameLinks

print gameLinks()


Which produces a list with this type of elements: <td class="name"><a href="/premier-league-2012-2013/arsenal-wigan-1405/">Arsenal - Wigan</a></td>


What I want to do is generate a new list containing certain pieces of this string. Specifically, I want to grab the four characters right before the /" inside the second html tag - so from this element, I'd like to grab 1405. What would the best way to proceed be?

--
You received this message because you are subscribed to the Google Groups "beautifulsoup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beautifulsou...@googlegroups.com.
To post to this group, send email to beauti...@googlegroups.com.
Visit this group at http://groups.google.com/group/beautifulsoup?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages