-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrent Price Data.py
36 lines (26 loc) · 1.27 KB
/
Current Price Data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
from bs4 import BeautifulSoup
import pandas as pd
def smartlab_info(url): # Current Stock Price Data
"""
Scrapes current stock price data from the given URL.
:param url: The URL of the webpage to scrape the stock price data.
:return: A DataFrame containing ticker and price information.
"""
# Send a request to the webpage
response = requests.get(url)
response.raise_for_status() # Check for request errors
# Find the first table on the page and extract all rows ('tr' tags)
table_rows = BeautifulSoup(response.content, 'html.parser').find_all('tr')
stock_data = []
# Iterate over the rows, starting from the second row (to skip the header)
for row in table_rows[1:]: # Skip the first row (header)
cells = row.find_all('td')
# Ensure the row contains enough data (e.g., 7 cells)
if len(cells) >= 7:
# Append ticker and price to the stock_data list
stock_data.append([cells[2].get_text(strip=True),
cells[6].get_text(strip=True)])
# Create a DataFrame from the stock data
return pd.DataFrame(stock_data, columns=["ticker", "price"])
smartlab_info("https://smart-lab.ru/q/shares/") # Test