-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.py
26 lines (22 loc) · 986 Bytes
/
crawl.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
import requests, urllib, urllib2, os
from bs4 import BeautifulSoup
import style
def getImg(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
imgs = soup.find_all('img')
for img in imgs:
print(img.get('src'))
def downloadImg(imgUrl, targetUrl):
arr = imgUrl.split('/')
fileName = arr[len(arr) - 1]
if not os.path.exists(targetUrl + fileName):
output = open(targetUrl + fileName, 'wb+')
imgData = urllib2.urlopen(imgUrl).read()
output.write(imgData)
output.close()
print style.use_style('[info] ', mode='bold', fore='green') + style.use_style(fileName, fore='cyan') + ' is downloaded.'
else:
print style.use_style('[warning] ', mode='bold', fore='red') + style.use_style(fileName, fore='purple') + ' is here!'
downloadImg('http://posters.imdb.cn/ren-pp/0000701/CjR3AsiaP_1190290948.jpg', '/Users/zhengmeiyu/Downloads/')
getImg('http://22mm.xiuna.com/mm/qingliang/')