我使用beautifulsoup4来获取股票数据并发送到python中的电子表格。我遇到的问题是我不能让我的循环跳过返回None的属性。所以我需要的是将空值添加到行的代码 属性将不返回。
//my dictionay for storing data
data = {
'Fiscal Quarter End' : [],
'Date Reported' : [],
'Earnings Per Share' : [],
'Consensus EPS* Forecast' : [],
'% Surprise' : []
}
url = ""
html = requests.get(url)
data = html.text
soup = bs4.BeautifulSoup(data)
table = soup.find("div", class_="genTable")
for row in table.find_all('tr')[1:]:
if row.has_attr('tr'):
cols = row.find_all("td")
data['Fiscal Quarter End'].append( cols[0].get_text() )
data['Date Reported'].append( cols[1].get_text() )
data['Earnings Per Share'].append( cols[2].get_text() )
data['Consensus EPS* Forecast'].append( cols[3].get_text() )
data['% Surprise'].append( cols[4].get_text() )
else:
//where i need to add in the empty 'n/a' values
data['Fiscal Quarter End'].append()
data['Date Reported'].append()
data['Earnings Per Share'].append()
data['Consensus EPS* Forecast'].append()
data['% Surprise'].append()
答案 0 :(得分:0)
您已将data
变量用于两个不同的事物。第二种用法覆盖了你的字典。在html.text
的调用中使用soup.find()
更为简单。请尝试以下方法:
import requests
import bs4
# My dictionary for storing data
data = {
'Fiscal Quarter End' : [],
'Date Reported' : [],
'Earnings Per Share' : [],
'Consensus EPS* Forecast' : [],
'% Surprise' : []
}
empty = 'n/a'
url = ""
html = requests.get(url)
soup = bs4.BeautifulSoup(html.text, "html.parser")
table = soup.find("div", class_="genTable")
rows = []
if table:
rows = table.find_all('tr')[1:]
for row in rows:
cols = row.find_all("td")
data['Fiscal Quarter End'].append(cols[0].get_text())
data['Date Reported'].append(cols[1].get_text())
data['Earnings Per Share'].append(cols[2].get_text())
data['Consensus EPS* Forecast'].append(cols[3].get_text())
data['% Surprise'].append(cols[4].get_text())
if len(rows) == 0:
# Add in the empty 'n/a' values if no columns found
data['Fiscal Quarter End'].append(empty)
data['Date Reported'].append(empty)
data['Earnings Per Share'].append(empty)
data['Consensus EPS* Forecast'].append(empty)
data['% Surprise'].append(empty)
如果table
或rows
为空,data
将保留以下内容:
{'Date Reported': ['n/a'], 'Earnings Per Share': ['n/a'], '% Surprise': ['n/a'], 'Consensus EPS* Forecast': ['n/a'], 'Fiscal Quarter End': ['n/a']}