S
S
space36942017-02-14 18:24:42
Python
space3694, 2017-02-14 18:24:42

How to access an element through BeautifulSoup?

Hello everyone, just started learning python.
There was a question on BeautifulSoup
How to access a certain element if it does not have a class set?
For example:

<div class="myclass">
    <h4><a href="#"> Текст ссылки </a></h4>
</div>

If the link has a class attribute, then everything is clear, we write:
soup.find_all('a', {'class':'class_name'):
And that's it.
But what if the class attribute is not registered, but you need to access exactly this link, which is located in div>h6?
Thanks in advance for your replies

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-02-14
@space3694

link = bs_obj.find('a', href=re.compile('regexp'))
link = bs_obj.find('div', {'class': 'className'}).h6.find_all('a')[0]

N
nirvimel, 2017-02-14
@nirvimel

CSS: div.myclass h4 a
XPath://div[contains(@class, 'myclass')]/h4/a

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question