Skip to content

Instantly share code, notes, and snippets.

@jsb9412
Created December 6, 2014 07:47
Show Gist options
  • Save jsb9412/b7f9adcf91a12861bcc6 to your computer and use it in GitHub Desktop.
Save jsb9412/b7f9adcf91a12861bcc6 to your computer and use it in GitHub Desktop.
Webtoon Download Program in Python
import re, sys
import urllib2
webtoonURL = 'http://comic.naver.com/webtoon/detail.nhn?titleId='
subWebtoonURL = '&no='
def saveHtml(content, filename) :
f = open(filename, 'w')
f.write(content)
f.close()
def getHtml(url) :
response = urllib2.urlopen(url)
return response.read()
def extractImgs(html) :
exp = re.compile(r'<img.+?src="(http://imgcomic\.naver\.net/webtoon/[0-9]+/[0-9]+/(.+?\.(jpg|png|gif)))".*?>')
imgs = exp.findall(html)
return imgs
def printImages(images) :
for img in images :
print images[0] # full link
print images[1] # file name
print images[2] # extension
return 0
def saveImage(imgName, link, index) :
print 'start saveImage function!'
fileName = imgName + str(index) + '.jpg'
f = open(fileName, 'wb')
print('link is ' + link)
response = urllib2.urlopen(link).read()
# content = response.read()
#print content
f.write(response)
f.close()
return 0
def saveWebtoon(titleID, htmlName, index, imgName) :
fullURL = webtoonURL + str(titleID) + subWebtoonURL + str(index)
print ('fullURL : ' + fullURL)
html = getHtml(fullURL)
htmlFullName = htmlName + '_' +str(index) + '.html'
saveHtml(html, htmlFullName)
f = open(htmlFullName, 'r')
html = f.read()
f.close()
images = extractImgs(html)
if len(images) == 0 :
print >> sys.stderr, "No Images!!"
return 1
firstURL = images[0][0][:22]
secondURL = images[0][0][25:-5]
fileNum = 1
for img in images :
currentURL = firstURL + 'com' + secondURL + str(fileNum) + '.jpg'
saveImage(imgName, currentURL, fileNum)
fileNum += 1
return 0
def main() :
# input about data
titleID = input('Enter TitleID : ')
finalEpisode = int(input('Enter Final Episode : '))
htmlName = titleID
imgName = input('Enter Image Name : ')
for i in range(1, finalEpisode) :
fullImageName = imgName + 'Episode' + str(i) + '_'
saveWebtoon(titleID, htmlName, i, fullImageName)
return 0
if __name__ == '__main__' :
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment