-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
60 lines (46 loc) · 1.34 KB
/
scrape.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import requests
from bs4 import BeautifulSoup
# Scrape website for pdf
def scrape():
result = ''
html = requests.get('http://www.oslobors.no/Oslo-Boers/Notering/Aksjer-egenkapitalbevis-og-retter-til-aksjer/Oslo-Boers-og-Oslo-Axess/Noterte-selskapers-hjemstat').text
bs = BeautifulSoup(html)
for link in bs.find_all('a'):
if (link.has_attr('href')):
l = link.attrs['href']
if ('.pdf' in l):
result = 'http://www.oslobors.no' + link.attrs['href']
return result
# Check if pdf is new
def status(url):
file = r'data/url.txt'
# If file does not exist
if not os.path.exists(file):
f = open(file, 'w')
f.write(url)
f.close()
return True
# if file does exist
else:
content = ''
with open(file, 'r') as f:
content = f.readline()
# If url is new
if (content != url):
f = open(file, 'w')
f.write(url)
f.close()
return True
# If url is old
else:
return False
# Download pdf
def download(url):
file = r'data/pdf/aksjer.pdf'
r = requests.get(url, stream=True)
with open(file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return file