Skip to content

Instantly share code, notes, and snippets.

@thunderInfy
Created May 24, 2021 23:00
Show Gist options
  • Save thunderInfy/2da449e906e81fac1dbe5a0f6192ba16 to your computer and use it in GitHub Desktop.
Save thunderInfy/2da449e906e81fac1dbe5a0f6192ba16 to your computer and use it in GitHub Desktop.
def construct_df(soup):
L = soup.findAll(True, {'class':['center-name-title','center-name-text', 'slots-box']})
# obtaining class specific information from soup results
M = []
for i in L:
class_names = i.get('class')
if class_names == ['center-name-title']:
ADD = i.get_text()
elif class_names == ['center-name-text']:
M.append((class_names, ADD + " " + i.get_text()))
else:
M.append((class_names, i.get_text()))
# constructing pandas dataframe
pushelement = None
data = []
rownames = []
for i in M:
if i[0] == ['center-name-text']:
rownames.append(i[1])
if pushelement is not None:
data.append(pushelement)
pushelement = []
else:
pushelement.append(i[1])
if pushelement is not None:
data.append(pushelement)
# Create the pandas DataFrame
df = pd.DataFrame(data)
df.index = rownames
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment