-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitg.py
52 lines (48 loc) · 1.68 KB
/
itg.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
import requests
from bs4 import BeautifulSoup
import urllib2
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
def download_file(file_name, download_url):
try:
req = urllib2.Request(download_url, headers=hdr)
response = urllib2.urlopen(req)
file = open(file_name + ".pdf", 'wb')
file.write(response.read())
file.close()
except urllib2.HTTPError, e:
print e.fp.read()
base_url = "https://www.google.com.sg/search?q=site:itg.com+filetype:pdf+trading"
prev_href = ""
for i in range(1000):
x = i * 10
url = base_url + "&start=%d" % x
print url
page = requests.get(url, verify=False)
soup = BeautifulSoup(page.content)
links = soup.findAll("a")
downloaded = False
for link in links:
pos = link["href"].find("http://www.itg.com")
href = ""
if pos >= 0:
href = link["href"][pos:]
else:
pos = link["href"].find("https://www.itg.com")
if pos >= 0:
href = link["href"][pos:]
end_pos = href.find(".pdf")
if end_pos < 0:
continue
href = href[:end_pos + 4]
if href != "" and href != prev_href:
print href
prev_href = href
download_file('./trading/' + href.split('/')[-1], href)
downloaded = True
if not downloaded:
break