U E D R , A S I H C RSS

권�기/web crawler


1. 목�


Python� �용해서 Web Crawler를 제작하면서 Python� 사용법� �히고, �하는 웹 페�지를 �기 위한 Web Crawler를 제작한다. (네�버웹툰(�아온 럭키짱, 신� 탑...), 네�버 �스트, 그 외� �종 웹페�지..)


2. 필요기술


  • HTML
  • CSS
  • JavaScript
  • Python

    HTML, CSS, JavaScript - 웹 페�지 분�
    Python


3. 진행 과정



3.1. 필요한 문서

3.2. 시작


3.2.1. 웹 페�지 소스 �어오기




import urllib
import urllib2

req = urllib2.Request('http://9632024.tistory.com/974')
try: urllib2.urlopen(req)
except URLError, e:
	print e.reason

fo = open("test1.html","w")
for line in urllib2.urlopen(req).readlines():
	fo.write(line)

fo.close()


3.2.2. 소스ì—�서 URLë§Œ ì¶”ìœí•˜ê¸°


import urllib
import urllib2
import string

fo1 = open("test1.html", "r")
fo2 = open("test2.html", "w")

for line in fo1.readlines() :
	pos = string.find(line, '"http')
	if pos is not -1 :
		for c in range(pos+1, len(line)) :
			if line[c] is '"' :
				fo2.write("\n")
				break
			fo2.write(line[c])

fo1.close()
fo2.close()

3.2.3. 파� 다운로드하기

import urllib
import urllib2


fo = open("test2.html", "r")
for line in fo.readlines():
	urllib.urlretrieve(line,line.split('/')[-1])

fo.close()



Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> first = 1
>>> second = 2
>>> first, second = second, first
>>> print first
2
>>> print second
1
>>> first, second = second, first
>>> third = 3
>>> first, second, third = third, first, second
>>> print first, second, third
3 1 2


3.2.4. 디렉토리 만들기


import os

os.chdir(os.getcwd() + '/folder')
def create_dir(folder):
	cdir = os.getcwd()
	mdir = cdir + folder
	print mdir;
	if os.path.isdir(mdir) is  False :
		os.mkdir(mdir , 0755)

type = ['/mp3', '/jpg', '/txt']
for t in type :
	create_dir(t)


  • os.chdir(path) - Change the current working directory to path.
  • os.getcwd() - Return a string representing the current working directory.
  • os.path.isdir(path) - Return True if path is an existing directory.
  • os.mkdir(path, mode) - Create a directory named path with numeric mode mode. If the directory already exists, OSError is raised.

    http://docs.python.org/library/os.html
    http://docs.python.org/library/os.path.html#module-os.path

  • mode -
    d - 디렉토리 구분
    r - �기 권한
    w - 쓰기 권한
    x - 실행 권한

    d / rwx / r-x / r-x
디렉토리 소유� 권한 그룹 권한 전체 권한
r(4)w(2)x(1)
755 -> drwxr-xr-x
http://snowbora.com/343

3.2.5. 웹툰(네�버) �기



import prepare
import os

currentpath = os.getcwd()
path = os.getcwd() + '/luckyzzang'
if os.path.isdir(path) is False :
	os.mkdir(path, 0755)


os.chdir(path)
currentpath = os.getcwd()


for i in range(1, 21):
	url = 'http://comic.naver.com/webtoon/detail.nhn?titleId=449854&no=' + str(i) + '&weekday=wed'

	path = currentpath + '/' + str(i)
	if os.path.isdir(path) is False :
		os.mkdir(path, 0755)
	
	os.chdir(path)
	
	prepare.readpage(url, str(i) + '.html')
	prepare.extractwt(str(i) + '.html', str(i) + 'file.html')
	prepare.download(str(i) + 'file.html')


3.2.6. wxPython


�꾸 글�만 나오는 환경� 보니까 질리기 시작했다. 아� 완성� 멀었지만 GUI로 만들어보려고 함. 어차피 나중� 해야�니까...

3.2.8. Eclipse + PyDev + wxPython + pywin32


1. Eclipse 설치
2. Eclipse�서, Help > Install New Software > Add > PyDev, Http://pydev.org/updates
3. 중간� Aptana 신뢰하는지� ��, 재시작
4. Window > Preference > PyDev > Interpreter - Python > Auto Config
5. New Folder > /usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode

  • 사실 ë�¼ì�´ë¸ŒëŸ¬ë¦¬ 다 깔아놓고 Auto Config하면 ë�¨..

3.3. 개선해야 할 �


  • 파ì�¼ì�„ 저장할 ë–„ 소스 파ì�¼ì—� 저장ì�´ ë�˜ì„œ 지저분하다. o
  • 필요하지 않ì�€ 파ì�¼ê¹Œì§€ ì „ë¶€ ê¸�어온다. o
  • ë‚´ê°€ ì�¸í„°ë„·ì—�서 jpg 파ì�¼ ê¸�어오려고 만든 파ì�´ì�¬ 코드 있는ë�° 혹시 필요함? - 서민관
    • 그러면 매우 ê°�사하죠 ã…Žã…Ž - ê¶Œì˜�기
  • http://git-scm.com/ - Git.
  • http://wiki.kldp.org/wiki.php/SubversionBook/BranchingAndMerging
  • http://www.crummy.com/software/BeautifulSoup/ - 웹분ì„� ë�¼ì�´ë¸ŒëŸ¬ë¦¬ BeautifulSoup ì�´ìš©í•´ë³´ê¸°
  • wxpython
  • http://docs.python.org/tutorial/classes.html / 9.5까지.
  • 로그ì�¸ì�´ 가능한 페ì�´ì§€ë¥¼ ê¸�ê³  ì‹ì�€ë�° 방법ì�„ 모르겠다.
  • êµ³ì�´ Python으로 GUI를 구현해야 í•  필요성ì�„ 모르겠다.

  • í�¬ë¡¤ëŸ¬ë�¼ë‹ˆ.. ì�Œ. -태진
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:46
Processing time 0.0094 sec