- 전체
- Python 일반
- Python 수학
- Python 그래픽
- Python 자료구조
- Python 인공지능
- Python 인터넷
- Python SAGE
- wxPython
- TkInter
- iPython
- wxPython
- pyQT
- Jython
- django
- flask
- blender python scripting
- python for minecraft
- Python 데이터 분석
- Python RPA
- cython
- PyCharm
- pySide
- kivy (python)
파이썬 코드 50라인으로 웹 크롤러 만들기
2012.10.05 15:59
파이썬 코드 50라인으로 웹 크롤러 만들기
[출처] http://theanti9.wordpress.com/2009/02/14/python-web-crawler-in-less-than-50-lines/
Python Web Crawler in Less Than 50 LinesI 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 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. |
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 5 |
파이선 애드온을 이용한 블랜더 2.6 : Creating a Blender 2.6 Python add-on
| 졸리운_곰 | 2017.08.15 | 483 |
| 4 | blender import/export script | 졸리운_곰 | 2017.08.13 | 1594 |
| 3 | blender python 퀵스타드 매뉴얼 : blender python quick start | 졸리운_곰 | 2017.08.13 | 1394 |
| 2 |
블랜더 파이썬 애드온 개발 : blender python addon dev
| 졸리운_곰 | 2017.08.13 | 3386 |
| 1 |
Python Scripting for the Blender Game Engine
| 졸리운_곰 | 2017.08.13 | 736 |

