I am using BeautifulSoup for my project
and I don't know how the object returned by the Soup is organized,
so I don't know how to access it, except using only print

I can only do this:

link = soup.find(attrs={'class' : re.compile("util2$")})
print link

it print out the whole <a href......> .... </a>
but I only want the address within it
do anybody knows?

Recommended Answers

All 7 Replies

You can find out how something is make up by using the dir() function.

This lists all of the variables, methods and classes of whatever you put inside the brackets :)

I do this:
link = soup.find(attrs={'class' : re.compile("util2$")})
print dir(link)

what appear is:

lamwaiman@cs6201:~/python> python m.py

OMG

I do this:
link = soup.find(attrs={'class' : re.compile("util2$")})
print dir(link)

what appear is:


OMG

You can import the module pprint, and using the function pprint, you can display each function and variable on its own line.

from pprint import pprint

link = soup.find(attrs={'class':re.compile("util2$")})
pprint(dir(link))

it can only shows the functions of the object///
not something I want.....

problem solved.......
I found out how to Navigate it.......

problem solved.......
I found out how to Navigate it.......

And are you going to share your insights, so that others could benefit from them, or keep them to yourself?

And are you going to share your insights, so that others could benefit from them, or keep them to yourself?

don't be mad.
Just in the doc of BeautifulSoup....

The attributes of Tags

Tag and NavigableString objects have lots of useful members, most of which are covered in Navigating the Parse Tree and Searching the Parse Tree. However, there's one aspect of Tag objects we'll cover here: the attributes.

SGML tags have attributes:. for instance, each of the <P> tags in the example HTML above has an "id" attribute and an "align" attribute. You can access a tag's attributes by treating the Tag object as though it were a dictionary:

firstPTag, secondPTag = soup.findAll('p')

firstPTag
# u'firstPara'

secondPTag
# u'secondPara'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.