[英]AttributeError: 'NoneType' object has no attribute 'find_all' Beautifulsoup
[英]'NoneType' object has no attribute 'find_all' with Beautifulsoup
我已經嘗試了我找到的這段代碼,但是它給了我 AttributeError 的錯誤消息:'NoneType' object has no attribute 'find_all' 我不熟悉 Beautifulsoup 並且不知道如何解決這個問題。 試圖找到一個解決方案,我忽略了 tabpane 部分,但無法弄清楚。 你有什么建議嗎?
import datetime
import pandas as pd # pip install pandas
import requests # pip install requests
from bs4 import BeautifulSoup # pip install beautifulsoup4
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0)
Gecko/20100101 Firefox/87.0',
}
url = 'https://www.marketwatch.com/tools/earningscalendar'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
tabpane = soup.find('div', 'tabpane')
earning_tables = tabpane.find_all('div', {'id': True})
dfs = {}
current_datetime = datetime.datetime.now().strftime('%m-%d-%y %H_%M_%S')
xlsxwriter = pd.ExcelWriter('Earning Calendar
({0}).xlsx'.format(current_datetime), index=False)
for earning_table in earning_tables:
if not 'Sorry, this date currently does not have any earnings
announcements scheduled' in earning_table.text:
earning_date = earning_table['id'].replace('page', '')
earning_date = earning_date[:3] + '_' + earning_date[3:]
print(earning_date)
dfs[earning_date] = pd.read_html(str(earning_table.table))[0]
dfs[earning_date].to_excel(xlsxwriter, sheet_name=earning_date,
index=False)
xlsxwriter.save()
print('earning tables Excel file exported')
抓取頁面中的所有表格:
tables = pd.read_html("https://www.marketwatch.com/tools/earnings-calendar")
只看第一個:
print(tables[0].head())
如果您確定所有表都具有相同的列,則可以將它們連接為只有一個數據框:
df = pd.concat(pd.read_html("https://www.marketwatch.com/tools/earnings-calendar"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.