-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre_download.py
executable file
·114 lines (92 loc) · 2.15 KB
/
pre_download.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
#!/usr/bin/env python
#coding=utf-8
from urllib2 import urlopen,ProxyHandler,install_opener,build_opener
from bs4 import BeautifulSoup
import sys
import re
import chardet
import codecs
import urllib
import mythread
'''
用法 python pre_down.py www.qq.com proxy_ip
'''
urls = []
def extract_css(css_url):
'''
解析css文件里面的图片链接等
'''
line = urlopen(css_url).read()
for l in re.findall(r'url\((.*?)\)',line):
if l.startswith('http://') :
urls.append(l)
def image_url(res,line):
'''
挑选图片,包括从css中,或者页面中
'''
for l in res.findall(line):
urls.append(l)
def extract_url(domain):
'''
将domain 首页里面所有的url进行提取,提取出所有的链接,放到urls
'''
line = urllib.urlopen(domain).read()
data = BeautifulSoup(line)
links = data.findAll('a')
for a in links:
try:
if a.has_attr('href'):
if a['href'].startswith('http://'):
urls.append(a['href'])
if a['href'].endswith('.css'):
extract_css(a['href'])
except:
print 'some error heppend'
#以下获取src的连接
try:
src = re.findall(r'src="(.*?)"',line)
for url in src:
if url.endswith('.css'):
extract_css(url)
else:
urls.append(url)
except:
pass
#以下获取url() 中的链接
try:
url = re.findall(r'url\((.*?)\)',line)
for u in url:
if u.startswith('http://'):
urls.append(u)
except:
pass
def pre_down(url):
'''
将url,通过设置代理进行预加载下来
'''
proxy = ProxyHandler({'http':'http://'+proxy_ip+':80'})
opener = build_opener(proxy)
install_opener(opener)
try:
urlopen(url)
print '%s 下载好了 ' %(url)
except:
pass
if __name__ == '__main__':
domain = ''
proxy_ip = ''
if len(sys.argv) > 2:
domain = sys.argv[1]
proxy_ip = sys.argv[2]
else:
print ' Useage: python %s domain proxy_ip ' %(sys.argv[0])
print ' Example: python pre_down.py www.sina.com.cn 221.130.162.34'
sys.exit(1)
if not domain.startswith('http://'):
domain = 'http://' + domain
extract_url(domain)
ok = []
for url in urls:
if url.startswith('http:'):
ok.append(url)
mythread.mythread(list(set(ok)),pre_down,40)