gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
can you provide code to pull stock data from finviz web site
- User
can you provide code to pull stock data from finviz web site
- Assistant
Sure! Here's an example code in Python using the BeautifulSoup library to scrape stock data from the Finviz website:
pythonimport requests from bs4 import BeautifulSoup def get_stock_data(stock_symbol): url = f"https://finviz.com/quote.ashx?t={stock_symbol}" # Send GET request response = requests.get(url) # Parse the HTML content soup = BeautifulSoup(response.content, "html.parser") # Find the table containing stock data table = soup.find("table", attrs={"class": "snapshot-table2"}) # Extract required data from the table data = {} rows = table.find_all("tr") for row in rows: key = row.find("td", attrs={"class": "snapshot-td2-cp"}).text value = row.find("td", attrs={"class": "snapshot-td2"}).text data[key] = value return data # Specify the stock symbol stock_symbol = "AAPL" # Get stock data stock_data = get_stock_data(stock_symbol) # Print the retrieved data for key, value in stock_data.items(): print(key + ": " + value)