pandas 如何将从soup收集的数据附加到dataframe?

dy1byipe  于 2023-01-01  发布在  其他
关注(0)|答案(3)|浏览(145)
car_names = soup.find_all("h3")
for name in car_names:
    print(name.text)

我需要将名称.文本保存到 Dataframe 中?

qyswt5oh

qyswt5oh1#

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

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

ev7lccsx2#

您可以使用下一个示例如何从HTML数据创建PandasDataFrame:

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)

图纸:

Car Name
0     Ford
1   Toyota
2    Škoda
ar7v8xwq

ar7v8xwq3#

我想你在找这个

import pandas as pd

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

相关问题