- 전체
- 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
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 17 | Tkinter Summary [Tkinter 요약] | 졸리운_곰 | 2014.10.14 | 3203 |
| 16 | An Introduction To Tkinter TkInter에 대한 소개 | 졸리운_곰 | 2014.10.14 | 2649 |
| 15 |
tkinter_nmt.pdf
| 졸리운_곰 | 2014.10.14 | 6234 |
| 14 |
tkinter.pdf
| 졸리운_곰 | 2014.10.14 | 6858 |
| 13 |
wxpython_in_action_wxact.pdf
| 졸리운_곰 | 2014.10.14 | 7113 |
| 12 |
wxPython 2.8 Application Development Cookbook (2010).pdf
| 졸리운_곰 | 2014.10.14 | 2773 |
| 11 |
The wxPython tutorial.pdf
| 졸리운_곰 | 2014.10.14 | 2875 |
| 10 |
tkinter-python-intro.pdf
| 졸리운_곰 | 2014.10.14 | 7646 |
| 9 |
python-and-tkinter-programming.pdf
| 졸리운_곰 | 2014.10.14 | 7918 |
| 8 | Python 문법정리 | 졸리운_곰 | 2014.05.28 | 4393 |
| 7 | python 으로 torrent 파일 컨텐츠 다운로드 | 졸리운_곰 | 2014.05.13 | 1797 |
| 6 |
NumMethodPython.pdf 파이썬 수치해석학 python numerical methods
| 졸리운_곰 | 2014.04.23 | 826 |
| 5 | 파이썬에서 url 인코딩 / 디코딩 | 졸리운_곰 | 2014.03.03 | 1501 |
| 4 | Python - Linux: Parse Network Stats From ifconfig | 졸리운_곰 | 2014.02.14 | 420 |
| 3 |
[python] mechanize 와 Beautifup soup를 이용한 웹 사이트 정보 수집
| 가을의 곰을... | 2012.10.11 | 5552 |
| 2 | [파이썬] scrapy 로 웹 사이트 크롤링 | 가을의 곰을... | 2012.10.09 | 8939 |
| » | 파이썬 코드 50라인으로 웹 크롤러 만들기 | 가을의 곰을... | 2012.10.05 | 9539 |

