'How can I distribute member to it's role on python in bs4 + lxml from site?

I want to parse a role-cards on python from nft-collectible site: https://imaginaryones.com

Here's my code:

find_title = soup.find("title")
find_all_card_titles = soup.findAll("div", class_="v-card__title")
find_all_names = soup.findAll("h4", class_="member__subtitle")
find_all_links_url = soup.findAll("a")

print('Name -', find_title.text)

for names in find_all_names:
    for roles in find_all_card_titles:
        print(names.text, '-', roles.text)

i get smth like:

Clement - Creator
Clement - Biz / Strategist
Clement - Artist / Partnerships
Clement - PM / Community
Clement - Tech / Contracts
David - Creator
David - Biz / Strategist
David - Artist / Partnerships
David - PM / Community
David - Tech / Contracts

etc...

but i need a result like:

Clement - Creator
David - Biz / Strategist
Gregory - Artist / Partnerships

etc...

How can i do this?



Solution 1:[1]

Try:

import requests
from bs4 import BeautifulSoup

url = "https://imaginaryones.com"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for member in soup.select(".team__list .member__subtitle"):
    print(member.text, "-", member.find_previous(class_="v-card__title").text)

Prints:

Clement - Creator
David - Biz / Strategist
Gregory - Artist / Partnerships
Caleb - PM / Community
Jerome - Tech / Contracts

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Kesely