-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownImg.py
55 lines (43 loc) · 1.16 KB
/
downImg.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib
import os, sys
import threading
import time
from bs4 import BeautifulSoup
if sys.hexversion >= 0x3000000:
import urllib.request as urlreq
else:
import urllib2 as urlreq
def main():
file = open('image.txt')
imgs = file.readlines()
file.close()
if not os.path.exists("download"):
os.makedirs("download")
downDir = os.getcwd()+"/download/"
print("downDir : ", downDir)
index = 0
for url in imgs:
index = index + 1
img = urlreq.urlopen(url)
fname = downDir+str(index).zfill(3)+".jpeg"
print("save to file: ", fname)
localfile = open(fname,'wb')
localfile.write(img.read())
img.close()
localfile.close()
return
def getImage(pagehref):
page = urlreq.urlopen(pagehref)
body = page.read().decode("utf-8", "replace")
page.close()
mhpic = ""
soup = BeautifulSoup(body, "lxml")
mhsrc = soup.find("img", {"oncontextmenu": 'return false'})
if mhsrc != None:
mhpic = mhsrc.get("src")
print("image source: ", mhpic)
return mhpic
if __name__ == "__main__":
main()