-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrap.py
44 lines (36 loc) · 1.71 KB
/
scrap.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
37
38
39
40
41
42
43
44
# requests-> obtain information from link , requests downloads html source code
import requests
# parse that information, helps target different tags ,attributes
from bs4 import BeautifulSoup
import time
import csv
from datetime import date
import send_mail
urls = ["https://finance.yahoo.com/quote/AMZN?p=AMZN&.tsrc=fin-srch",
"https://finance.yahoo.com/quote/GOOGL?p=GOOGL&.tsrc=fin-srch", "https://finance.yahoo.com/quote/MSFT?p=MSFT&.tsrc=fin-srch"]
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'}
today = str(date.today()) + ".csv"
csv_file = open(today, "w")
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Stock Name', 'Current Price', 'Previous Close', 'Open',
'Bid', 'Ask', 'Day Range', '52 Week Range', 'Volume', 'Avg. Volume'])
for url in urls:
stock = []
html_page = requests.get(url, headers=headers)
soup = BeautifulSoup(html_page.content, 'lxml')
header_info = soup.find_all("div", id="quote-header-info")[0]
stock_title = header_info.find("h1").get_text()
current_price = header_info.find(
"div", class_="My(6px) Pos(r) smartphone_Mt(6px)").find("span").get_text()
stock.append(stock_title)
stock.append(current_price)
table_info = soup.find_all(
"div", class_="D(ib) W(1/2) Bxz(bb) Pend(12px) Va(t) ie-7_D(i) smartphone_D(b) smartphone_W(100%) smartphone_Pend(0px) smartphone_BdY smartphone_Bdc($seperatorColor)")[0].find_all("tr")
for i in range(0, 8):
value = table_info[i].find_all("td")[1].get_text()
stock.append(value)
csv_writer.writerow(stock)
time.sleep(5)
csv_file.close()
send_mail.send(filename=today)