-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsakhr-archive-scraper.py
50 lines (36 loc) · 1.27 KB
/
sakhr-archive-scraper.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
__author__ = 'mosab'
import os
import urllib
import requests
from lxml import html
def grab_issue_html(issue_id):
url = 'http://archivebeta.sakhrit.com/newPreview.aspx?ISSUEID={}'.format(issue_id)
r = requests.get(url)
if r.status_code == 200:
return r.content
else:
# raise Er
pass
def extract_images(issue_html):
tree = html.fromstring(issue_html)
issue_images = tree.xpath('//div[contains(@class, "slide")]/img/@src')
for n, i in enumerate(issue_images):
image_path = i.replace('\\\\', '/').replace('\\', '/')
issue_images[n] = 'http://archivebeta.sakhrit.com/{}'.format(image_path)
return issue_images
def download_issue_images(issue_id, issue_images):
issue_dir = '{}'.format(issue_id)
if not os.path.exists(issue_dir):
os.makedirs(issue_dir)
for image in issue_images:
file_name = image.split('/')[-1]
file_full_name = '{}/{}'.format(issue_dir, file_name)
print 'Downloading file : "{}"'.format(file_full_name)
urllib.urlretrieve(image, file_full_name)
if __name__ == '__main__':
start = 1
end = 20000
for i in range(start, end):
content = grab_issue_html(i)
images = extract_images(content)
download_issue_images(i, images)