Python 美化组:合并表并导出到.csv
我一直在尝试从不同的URL下载数据,然后将其保存到csv文件中 其想法是从以下数据中提取年度/季度数据: 年度: 季度: 使用以下代码:Python 美化组:合并表并导出到.csv,python,python-3.x,web-scraping,beautifulsoup,export-to-csv,Python,Python 3.x,Web Scraping,Beautifulsoup,Export To Csv,我一直在尝试从不同的URL下载数据,然后将其保存到csv文件中 其想法是从以下数据中提取年度/季度数据: 年度: 季度: 使用以下代码: import requests import pandas as pd urls = ['https://www.marketwatch.com/investing/stock/AAPL/financials/cash-flow', 'https://www.marketwatch.com/investi
import requests
import pandas as pd
urls = ['https://www.marketwatch.com/investing/stock/AAPL/financials/cash-flow',
'https://www.marketwatch.com/investing/stock/MMM/financials/cash-flow']
def main(urls):
with requests.Session() as req:
goal = []
for url in urls:
r = req.get(url)
df = pd.read_html(
r.content, match="Cash Dividends Paid - Total")[0].iloc[[0], 0:3]
goal.append(df)
new = pd.concat(goal)
print(new)
main(urls)
输出:
我可以提取所需的信息(在示例中,针对2家公司的2015年和2016年),但仅针对1组(季度或年度)
我想合并表格年度+季度
为此,我在这段代码中想到:
import requests
import pandas as pd
from urllib.request import urlopen
from bs4 import BeautifulSoup
import csv
html = urlopen('https://www.marketwatch.com/investing/stock/MMM/financials/')
soup = BeautifulSoup(html, 'html.parser')
ids = ['cash-flow','cash-flow/quarter']
with open("news.csv", "w", newline="", encoding='utf-8') as f_news:
csv_news = csv.writer(f_news)
csv_news.writerow(["A"])
for id in ids:
a = soup.find("Cash Dividends Paid - Total", id=id)
csv_news.writerow([a.text])
但在获得以下错误时:
BeautifulSoup元素没有属性
text
,而是一个方法get\u text()
BeautifulSoup元素没有属性
text
,而是一个方法get\u text()
这意味着您的
soup.find()
未找到所需元素<代码>a为无
为什么需要
id
?我在5月19日查阅了年刊。无需使用id
这意味着您的汤。find()
未找到所需的元素<代码>a为无
为什么需要id
?我在5月19日查阅了年刊。不需要使用id
csv_news.writerow([a.get_text()])