-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrapper.py
185 lines (146 loc) · 4.45 KB
/
scrapper.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from bs4 import BeautifulSoup # sudo easy_install BeautifulSoup4
import time
import requests # sudo easy_install pip, pip install requests
import urllib
import os
import re
import cgi
import mechanize # sudo easy_install mechanize
import cookielib
from selenium import webdriver # sudo easy_install selenium
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
# Define colors for print out
class bcolors:
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
END = '\033[0m'
def printer(str, color, ends = False):
if ends:
print color + "---------- " + str + " ----------" + bcolors.END
else:
print color + str + bcolors.END
# Main Function
def scrapper():
global _dir
global start_i
global end_i
global base_url
global sub_url
global br
global cj
printer('STARTING SCRAPPER', bcolors.GREEN, True);
printer('Downloading to: '+_dir, bcolors.GREEN, False);
index = start_i
while index < end_i + 1:
page_url = sub_url + 'Page-' + str(index);
if index == 1:
page_url = 'http://www.pixeden.com/web-design-templates/'
printer('Starting Page: ' + page_url, bcolors.GREEN, False);
page_soup = BeautifulSoup(requests.get(page_url).text) # Grab Source from Page
item_divs = page_soup.findAll("div", { "class" : "itemContainer" })
for item in item_divs:
item_url = base_url + item.find('a')['href']
print '\n'
printer('Downloading Asset: ' + item_url, bcolors.YELLOW, True)
try:
resp = br.open(item_url);
item_soup = BeautifulSoup(resp.read())
download_div = item_soup.findAll("div",{"id": "download"})
download_url = base_url + download_div[0].find('a')['href']
print "download: " + download_url
f = urllib.urlopen(download_url)
try:
_, params = cgi.parse_header(f.headers.get('Content-Disposition', ''))
zip_name = params['filename']
except Exception:
zip_name = item.find('a')['href'].split('/')[2] + '.zip'
with open(zip_name, "wb") as PSD:
PSD.write(f.read()) # Save our File
printer('Finished Downloading: ' + zip_name, bcolors.GREEN, True)
sleep(5)
except Exception, e:
print e
printer('Error Downloading: ' + item_url, bcolors.RED, True)
print download_div
print '\n'
index+=1
printer('SCRAPPER HAS FINISHED', bcolors.GREEN, True);
second_try()
def second_try(): # have to still implement
global failed_downloads
# End Index
def get_end(_end):
global source_soup
_max = 1
line = source_soup.find('li', {"class":"pagination-end"})
a = line.find('a')
_max = int(re.findall(r'\d+', a['href'])[0])
if(_end <= 0):
return _max
if(_end>=_max):
return _max
else:
return _end
# Define our Globals
global _dir
global base_url
global start_i # Start Page Index
global end_i # End Page Index
global source_code
global source_soup
# Directory Setup
_dir = os.path.dirname(os.path.realpath(__file__)) + '/dump/'
if not os.path.exists(_dir):# Make our Initial Directory Comic Dump
os.makedirs(_dir)
os.chdir(_dir)
# Define Inital URLs
base_url = 'http://www.pixeden.com';
sub_url = base_url+'/latest/';
# /free-web-design-templates/
# /web-design-templates/
# /print-graphic-design-templates
# Get our HTML to scrape and create a Soup Object
source_code = requests.get(sub_url)
source_soup = BeautifulSoup(source_code.text)
# Define Inital Variables
start_i = 1
end_i = get_end(-1)
# Login to PixeDen.com
global br
global cj
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
username = '*******'
password = '*******'
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]
br.open(base_url)
formcount=0
for frm in br.forms():
if str(frm.attrs["id"])=="form-login":
break
formcount=formcount+1
br.select_form(nr=formcount)
br.form['username'] = username
br.form['passwd'] = password
br.submit()
# Check if we're logged in
if(br.response().read().find(username) != -1): # Logged In
printer("Logged in as " + username, bcolors.GREEN, True)
# Execute Scrapper
scrapper()
else:# Could not log in
printer("Oh No! You could not be logged in!", bcolors.RED, True)