Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noticias da B3 #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions my_scraper_project/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHROMEDRIVER_PATH=/path/to/chromedriver
Binary file added my_scraper_project/__pycache__/run.cpython-310.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions my_scraper_project/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Flask


def create_app():
app = Flask(__name__)
with app.app_context():
from . import routes
return app
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions my_scraper_project/app/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask import jsonify, current_app
from .scraper import scrape_news


@current_app.route('/scrape', methods=['GET'])
def scrape_and_save():
try:
print("Iniciando raspagem e salvamento de notícias...")
news = scrape_news()
print("Notícias raspadas:", news)
return jsonify({'message': 'News scraped and saved successfully', 'news': news}), 200
except Exception as e:
current_app.logger.error("Erro ao raspar as notícias: %s", e)
return jsonify({'message': 'Erro ao raspar as notícias'}), 500
35 changes: 35 additions & 0 deletions my_scraper_project/app/scraper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.by import By


def scrape_news():
print("Iniciando raspagem de notícias...")

print("Configurando Selenium...")

service = Service(ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=chrome_options)

print("Acessando URL...")
url = 'https://www.spacemoney.com.br/ultimas-noticias'
driver.get(url)

news = []
elements = driver.find_elements(By.XPATH, '//div[@class="linkNoticia crop"]')
for element in elements[:5]:
title = element.text
link = element.find_element(By.TAG_NAME, 'a').get_attribute('href')
news.append({'title': title, 'link': link})

driver.quit()

print("Raspagem de notícias concluída.")
print("Notícias encontradas:", news)

return news
5 changes: 5 additions & 0 deletions my_scraper_project/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Flask
requests
selenium
python-dotenv
webdriver_manager
28 changes: 28 additions & 0 deletions my_scraper_project/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#import os
from threading import Thread
import time
import requests
from app import create_app
import logging

logging.basicConfig(level=logging.INFO)

app = create_app()


def run_flask():
app.run(host='0.0.0.0', port=5005)


if __name__ == '__main__':
t = Thread(target=run_flask)
t.start()

time.sleep(5)

try:
response = requests.get('http://127.0.0.1:5005/scrape')
response.raise_for_status()
print(response.json())
except requests.exceptions.RequestException as e:
logging.error("Erro ao fazer a requisição GET: %s", e)