-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinviz_insider_trades.py
More file actions
executable file
·46 lines (35 loc) · 1.51 KB
/
Copy pathfinviz_insider_trades.py
File metadata and controls
executable file
·46 lines (35 loc) · 1.51 KB
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
37
38
39
40
41
42
43
44
45
46
import pandas as pd
from bs4 import BeautifulSoup as soup
from urllib.request import Request, urlopen
pd.set_option('display.max_colwidth', 60)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
# Define a function to scrape insider trades data from finviz.com
def get_insider_trades():
try:
# Set up the scraper
url = "https://finviz.com/insidertrading.ashx"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
html = soup(webpage, "html.parser")
# Extract the insider trades table
trades = pd.read_html(str(html), attrs={'class': 'body-table'})[0]
# Rename columns for readability
trades.columns = ['Ticker', 'Owner', 'Relationship', 'Date', 'Transaction',
'Cost', '#Shares', 'Value', '#Shares Total', 'SEC Form 4']
# Sort trades by date in descending order
trades = trades.sort_values('Date', ascending=False)
# Set the date column as index
trades = trades.set_index('Date')
# Remove the original date column from dataframe
trades = trades.drop('Date', axis=1)
# Remove the first two rows as they are not needed
trades = trades.iloc[2:]
# Return the first five rows of insider trades
return trades.head()
except Exception as e:
# Return the error message if any
return e
# Call the function and print the result
print('\nInsider Trades:')
print(get_insider_trades())