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 scrape tags from event names ex: AI, Big Data, ML etc.
My code:
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' ]
My expectation:
tag_list = ['Artificial Intelligence', 'Artificial Intelligence,Machine Learning, Healthcare', 'Artificial Intelligence,Public Sector' ]
Any help is appreciated.
Sorry if the question is too basic.
2
Answers
Replace the inner loop with a generator that you join into a string.
Here is the working solution:
Code:
OUTPUT:
… so on