Body.py:
import requests
import yfinance as yf
import csv
from bs4 import BeautifulSoup
tick = yf.Ticker("TSLA")
news = tick.news
news_array = []
for article in news:
link = article["link"]
try:
article_response = requests.get(link)
soup = BeautifulSoup(article_response.text, "html.parser")
news_body = soup.find("div", attrs={'class': 'caas-body'})
if news_body:
news_text = news_body.get_text(separator='\n\n')
else:
news_text = "No article found"
news_array.append(news_text)
except:
continue
csv_file = "news_articles.csv"
with open(csv_file, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(["News Body"])
for news_text in news_array:
writer.writerow([news_text])
print(f"News articles have been saved to '{csv_file}'.")
No comments:
Post a Comment