A
A
activebomb2019-07-20 16:27:50
Python
activebomb, 2019-07-20 16:27:50

How to parse not the first link, but all links from the code?

The problem is this:

def bf_parse(base_url, headers):
    session = requests.session()
    request = session.get(base_url, headers=headers)
    if request.status_code == 200:
        soup = bs(request.content, 'html.parser')
        divs = soup.find_all('div', attrs=('forum-user-name'))
        for div in divs:
            users = soup.find('div', {'class': 'forum-user-name'}).find('a').get('href')
            print(users)
        else:
            print('ERROR')

bf_parse(base_url, headers)

Here is the code itself, it parses links to users from the page, but it parses only one, the first link, I don’t know what to do with it, I tried find_all instead of find, it didn’t help either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Karo, 2019-07-20
@activebomb

It seems that the jamb in the logic is in this line
Here, the first matching div.forum-user-name is searched for on the entire page, but we need to search only in the block we are iterating over.
I can’t tell you exactly the solution, I don’t know the language
Probably something like
users = div.find('a').get('href')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question