Delete news_crawler.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243import requests
from bs4 import BeautifulSoup
def fetch_market_news(asset_name):
"""
Crawls news for the specific asset to provide context.
Using a simple crawler for demonstration purposes.
In a production bot, one would use specialized APIs like CryptoPanic or NewsAPI.
"""
search_query = asset_name.replace(" ", "+")
url = f"https://www.google.com/search?q={search_query}+market+news&tbm=nws"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
try:
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
news_items = []
# Google news results usually have h3 tags for titles
for item in soup.find_all('div', {'class': 'SoN15'}): # Update based on current Google News structure or use an API
title = item.find('div', {'role': 'heading'}).text if item.find('div', {'role': 'heading'}) else "No Title"
snippet = item.find('div', {'class': 'GI74ad'}).text if item.find('div', {'class': 'GI74ad'}) else ""
news_items.append({"title": title, "snippet": snippet})
if not news_items:
# Fallback to a simpler selector if Google changes classes
for g in soup.find_all('div', {'class': 'g'}):
title = g.find('h3').text if g.find('h3') else "No Title"
news_items.append({"title": title, "snippet": ""})
return news_items[:5] # Return top 5 news items
except Exception as e:
print(f"Error fetching news for {asset_name}: {e}")
return []
if __name__ == "__main__":
print("Testing News Crawler for BTC...")
news = fetch_market_news("Bitcoin")
for n in news:
print(f"- {n['title']}")news_crawler.pyDeleted-43