I have a for loop in python which extracts data using beautifulsoup from a website and appends them into a list. I am trying to scrap tags from event names ex: AI, Big Data, ML etc.
import requests from bs4 import BeautifulSoup URL = "https://aiml.events/" page = requests.get(URL) soup = BeautifulSoup(page.content, 'lxml') # Scrape Event Tags event_tags_list =  event_tag_div = soup.find_all('div', class_ = 'card-body') for event_div in event_tag_div: event_span = event_div.find_all('span', class_ = 'badge badge-light badge-pill') for event_tags in event_span: print(event_tags.text)
I am able to fetch the tags but they are all independent. I want to be able to group them together. Currently my list is like this:
tag_list = ['Artificial Intelligence', 'Artificial Intelligence','Machine Learning', 'Healthcare', 'Artificial Intelligence','Public Sector' ]
tag_list = ['Artificial Intelligence', 'Artificial Intelligence,Machine Learning, Healthcare', 'Artificial Intelligence,Public Sector' ]
Any help is appreciated. Sorry if the question is too basic.
Replace the inner loop with a generator that you join into a string.
for event_div in event_tag_div: event_span = event_div.find_all('span', class_ = 'badge badge-light badge-pill') event_tag_list.append(','.join(event_tag.text for event_tag in event_span))