파이썬 코드 50라인으로 웹 크롤러 만들기

[출처] http://theanti9.wordpress.com/2009/02/14/python-web-crawler-in-less-than-50-lines/

Python Web Crawler in Less Than 50 Lines

I got kind of bored today, and wrote a pretty simple web crawler with python and it turned out to be less than 50 lines. It doesn’t store output, I’ll leave that up to anyone who wants to use the code, because, well, theres just too many ways to choose from. Right now you pass it a starting link as a parameter and it will crawl forever untill it runs out of links. But that is not a likely condition. So here ya go. Have fun. Feel free to ask questions

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

import sys
import re
import urllib2
import urlparse
tocrawl = set([sys.argv[1]])
crawled = set([])
keywordregex = re.compile('<meta\sname=["\']keywords["\']\scontent=["\'](.*?)["\']\s/>')
linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')

while 1:
	try:
		crawling = tocrawl.pop()
		print crawling
	except KeyError:
		raise StopIteration
	url = urlparse.urlparse(crawling)
	try:
		response = urllib2.urlopen(crawling)
	except:
		continue
	msg = response.read()
	startPos = msg.find('<title>')
	if startPos != -1:
		endPos = msg.find('</title>', startPos+7)
		if endPos != -1:
			title = msg[startPos+7:endPos]
			print title
	keywordlist = keywordregex.findall(msg)
	if len(keywordlist) > 0:
		keywordlist = keywordlist[0]
		keywordlist = keywordlist.split(", ")
		print keywordlist
	links = linkregex.findall(msg)
	crawled.add(crawling)
	for link in (links.pop(0) for _ in xrange(len(links))):
		if link.startswith('/'):
			link = 'http://' + url[1] + link
		elif link.startswith('#'):
			link = 'http://' + url[1] + url[2] + link
		elif not link.startswith('http'):
			link = 'http://' + url[1] + '/' + link
		if link not in crawled:
			tocrawl.add(link)

** EDIT **

This was a very early draft of this program. As it turns out, I revisited this project a few months later and it evolved much more.
If you would like to check out the more evolved form, feel free to have a look here at my github

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
17 PyCLIPS Python Module file 졸리운_곰 2017.05.31 1481
16 A Flask API for serving scikit-learn models file 졸리운_곰 2017.05.31 589
15 tensorflow-kr.pdf [텐서플로우 튜토리얼] 한글 번역본 file 졸리운_곰 2016.11.25 4476
14 TensorFlow의 기본 사용법 졸리운_곰 2016.11.25 604
13 텐서플로우 시작하기 file 졸리운_곰 2016.11.25 773
12 Integrating Python and R Part III: An Extended Example file 졸리운_곰 2016.11.16 527
11 Integrating Python and R Part II – Executing R from Python and Vice Versa 졸리운_곰 2016.11.16 1543
10 Integrating Python and R into a Data Analysis Pipeline – Part 1 졸리운_곰 2016.11.16 635
9 Calling R from Python file 졸리운_곰 2016.11.16 450
8 TensorFlow의 기본 사용법 졸리운_곰 2016.06.11 725
7 텐서플로우 시작하기 file 졸리운_곰 2016.06.11 774
6 기계 학습의 A to Z : 인구 변화 시각화와 예측 file 졸리운_곰 2015.05.21 1736
5 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습(문서 분류) file 졸리운_곰 2015.05.21 2402
4 기계 학습의 A to Z : IPython notebook과 scikit-learn을 활용한 기계 학습 file 졸리운_곰 2015.05.21 2006
3 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습 (군집화, Clustering) file 졸리운_곰 2015.05.21 2085
2 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습 (차원 축소) file 졸리운_곰 2015.05.21 2182
1 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습(모델 평가) [1] file 졸리운_곰 2015.05.21 1411
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED