当我尝试使用 BeautifulSoup 学习网页抓取时,在使用 .append() 函数将数据插入字典(netflix_data)时遇到一些问题
这是我的整个源代码
import pandas as pd
import requests
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/netflix_data_webpage.html"
page = requests.get(url).text
soup = BeautifulSoup(page, "html.parser")
netflix_data = pd.DataFrame(columns=["Date", "Open", "High", "Low", "Close", "Adj_close", "Volume"])
for row in soup.find("tbody").find_all('tr'):
col = row.find_all("td")
date = col[0].text
open = col[1].text
high = col[2].text
low = col[3].text
close = col[4].text
adj_close = col[5].text
volume = col[6].text
netflix_data = netflix_data.append({
"Date": date,
"Open": open,
"High": high,
"Low": low,
"Close": close,
"Adj_close": adj_close,
"Volume": volume
}, ignore_index=True)
netflix_data.head()
然后就是结果
AttributeError Traceback (most recent call last)
<ipython-input-1-0e5fe86ede64> in ?()
19 close = col[4].text
20 adj_close = col[5].text
21 volume = col[6].text
22
---> 23 netflix_data = netflix_data.append({
24 "Date": date,
25 "Open": open,
26 "High": high,
c:\Users\haryy\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\generic.py in ?(self, name)
6295 and name not in self._accessors
6296 and self._info_axis._can_hold_identifiers_and_holds_name(name)
6297 ):
6298 return self[name]
-> 6299 return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'append'
我可以做什么来解决这个问题?谢谢
从 pandas 2 开始,您需要使用 pd.concat():
netflix_data = pd.concat([netflix_data, pd.DataFrame({
"Date": date,
"Open": open,
"High": high,
"Low": low,
"Close": close,
"Adj_close": adj_close,
"Volume": volume
})], ignore_index=True)