Abdurehman Dar
Abdurehman Dar

Reputation: 77

How do I append data that i gathered from soup to dataframe?

car_names = soup.find_all("h3")
for name in car_names:
    print(name.text)

I need to save name.text into a dataframe?

Upvotes: 0

Views: 71

Answers (3)

Ghost Ops
Ghost Ops

Reputation: 1734

I think you are looking for this

import pandas as pd

car_names = soup.find_all("h3")
res = [name.text for name in car_names]
df = pd.DataFrame(res)  
print(df)

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195428

You can use next example how to create pandas DataFrame from the HTML data:

import pandas as pd
from bs4 import BeautifulSoup

html_doc = """
<h3>Ford</h3>
<h3>Toyota</h3>
<h3>Škoda</h3>
"""

soup = BeautifulSoup(html_doc, "html.parser")

car_names = soup.find_all("h3")

data = []
for name in car_names:
    data.append({"Car Name": name.text})

df = pd.DataFrame(data)
print(df)

Prints:

  Car Name
0     Ford
1   Toyota
2    Škoda

Upvotes: 0

Amith Lakkakula
Amith Lakkakula

Reputation: 516

import pandas as pd
names = []
for name in car_names:
    names.append(name.text)

df =pd.DataFrame(data = names, columns = ['car_name'])

Upvotes: 1

Related Questions