Skip to content

Commit

Permalink
move downloader.py os.remove to clean.py etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
minghu6 committed Apr 17, 2017
1 parent 5e1c79e commit 6151e3c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
5 changes: 5 additions & 0 deletions weibo.cn/downloader/clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

import os
for fn in os.listdir("./captchas/"):
if os.path.splitext(fn)[1] == '.gif':
os.remove("./captchas/"+fn)
60 changes: 29 additions & 31 deletions weibo.cn/downloader/downloader.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
# coding:utf-8
import requests
import uuid
from PIL import Image
import os
from bs4 import BeautifulSoup

url = "http://login.weibo.cn/login/"
for i in range(2000):
try:
resp = requests.get(url)
bsObj = BeautifulSoup(resp.content, "lxml")
image_url = str(bsObj.img['src'])
resp = requests.get(image_url)
filename = str(uuid.uuid4()) + ".gif"
with open("./captchas/" + filename, 'wb') as f:
for chunk in resp.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
f.close()
im = Image.open("./captchas/" + filename)
try:
im = Image.open("./captchas/" + filename)
im.save("./captchas/" + filename.split('.gif')[0] + ".png")
except Exception, ex:
print Exception, ":", ex
os.remove("./captchas/" + filename)
print filename
except Exception, ex:
print Exception, ":", ex
# coding:utf-8
import requests
import uuid
from PIL import Image
import os
from bs4 import BeautifulSoup

url = "http://login.weibo.cn/login/"
for i in range(2000):
try:
resp = requests.get(url)
bsObj = BeautifulSoup(resp.content, "html.parser")
image_url = str(bsObj.img['src'])
#print(image_url)
resp = requests.get(image_url)
filename = str(uuid.uuid4()) + ".gif"
with open("./captchas/" + filename, 'wb') as f:
f.write(resp.content)

try:
with Image.open("./captchas/" + filename) as im:
im.save("./captchas/" + filename.split('.gif')[0] + ".png")

except Exception as ex:
print(Exception, ":", ex)
#os.remove("./captchas/" + filename)
print(filename)
except Exception as ex:
print(Exception, ":", ex)

0 comments on commit 6151e3c

Please sign in to comment.