파이썬 마을 게시판 인덱스 파이썬 마을
우리나라 파이썬 사용자들의 이야기 마을
 
 FAQFAQ   검색검색   멤버리스트멤버리스트   사용자 그룹사용자 그룹   사용자 등록하기사용자 등록하기 
 개인 정보개인 정보   비공개 메시지를 확인하려면 로그인하십시오비공개 메시지를 확인하려면 로그인하십시오   로그인로그인 
Google
python.or.kr Web

[질문] urllib 모듈로 그림까지 가져오려면??

 
글 쓰기   답변 달기    파이썬 마을 게시판 인덱스 -> 파이썬 질문과 답변
이전 주제 보기 :: 다음 주제 보기  
글쓴이 메시지
강성국
손님





올리기올려짐: 2001 9월 16 1:40 pm    주제: [질문] urllib 모듈로 그림까지 가져오려면?? 인용과 함께 답변

#snow.py

import urllib

ok = urllib.urlopen("http://www.snowcat.co.kr/diary/0109/0915.html")

print ok.read()

이걸 웹상에서 브라우져로 실행을 시켰거든요.

그런데 그림을 못가져오네요..

그림을 가져오려면 어떻게 해야지요?
위로
   
장혜식
손님





올리기올려짐: 2001 9월 16 11:18 pm    주제: [RE][질문] urllib 모듈로 그림까지 가져오려면?? 인용과 함께 답변

HTML을 얻어온 후 적절한 파싱을 거쳐서 일일이 img태그를
뽑아낸 다음 src를 다시 urlopen해야합니다.

------------------------------------------------------------
>#snow.py

import urllib

ok = urllib.urlopen("http://www.snowcat.co.kr/diary/0109/0915.html")

print ok.read()

이걸 웹상에서 브라우져로 실행을 시켰거든요.

그런데 그림을 못가져오네요..

그림을 가져오려면 어떻게 해야지요?
위로
   
강성국
손님





올리기올려짐: 2001 9월 17 7:23 am    주제: [RE] 답변 고맙습니다. 인용과 함께 답변

답변해주셔서 감사합니다.

------------------------------------------------------------
>HTML을 얻어온 후 적절한 파싱을 거쳐서 일일이 img태그를
뽑아낸 다음 src를 다시 urlopen해야합니다.

------------------------------------------------------------
>#snow.py

import urllib

ok = urllib.urlopen("http://www.snowcat.co.kr/diary/0109/0915.html")

print ok.read()

이걸 웹상에서 브라우져로 실행을 시켰거든요.

그런데 그림을 못가져오네요..

그림을 가져오려면 어떻게 해야지요?
위로
   
손님






올리기올려짐: 2001 9월 18 9:30 am    주제: [RE] 참고하세요 예제 소스입니다. 인용과 함께 답변

그냥 한번 예제로 만들어 봤습니다.

간단하게 만든거라 몇가지 처리하지 않은 문제점이 있습니다.
* index같이 디렉토리에 바로 파일이 딸려 나오는 경우 로컬에서 파일을
못 엽니다. => filepath가 파일이름으로 끝나지 않는 경우 처리해주면 됩니다.
* recursive depth제한이 없습니다.
* src에 소스가 들어간 태그 외에는 반응하지 않습니다.
* ''로 감싸져 있는 등 몇몇 형식에 잘못된 동작을 합니다.

필요하시다면 직접 고쳐서 쓰세요 Smile

코드:
#! /usr/local/bin/python
# ex:ts=4

import urllib, urlparse
import re, os.path, os

urlfind1 = re.compile(r'[<](\w*)[^>]*[Ss][Rr][Cc]=([^"\' ]\S*)[^>]*[>]')
urlfind2 = re.compile(r'[<](\w*)[^>]*[Ss][Rr][Cc]=["\']([^"\']*)["\'][^>]*[>]')

def recurget(url, got=None, depth=0):
    if got is None: got = []

    print "   "*depth + "Getting %s ..." % url
    content = urllib.urlopen(url).read()
    urls = urlfind1.findall(content) + urlfind2.findall(content)
    filepath = urlparse.urlparse(url)[2][1:]
    try:
        os.makedirs(os.path.dirname(filepath))
    except OSError:
        pass
    open(filepath, "w").write(content)

    for tag, target in urls:
        turl = urlparse.urljoin(url, target)
        print "   "*depth + " + Tag found (%s to %s) => %s" % (tag, target, turl)
        if turl not in got:
            got.append(turl)
            recurget(turl, got, depth+1)

if __name__ == "__main__":
    import sys
    for url in sys.argv[1:]:
        recurget(url)



실행하면 다음과 같이 됩니다.

kornet(perky):~/test/urlget>$ python urlget.py http://www.yahoo.co.kr/index.html
Getting http://www.yahoo.co.kr/index.html ...
+ Tag found (img to http://img.yahoo.co.kr/images/mani2.gif) => http://img.yahoo.co.kr/images/mani2.gif
Getting http://img.yahoo.co.kr/images/mani2.gif ...
+ Tag found (img to http://img.yahoo.co.kr/images/bul.gif) => http://img.yahoo.co.kr/images/bul.gif
Getting http://img.yahoo.co.kr/images/bul.gif ...
+ Tag found (img to http://img.yahoo.co.kr/images/shop_i.gif) => http://img.yahoo.co.kr/images/shop_i.gif
Getting http://img.yahoo.co.kr/images/shop_i.gif ...
+ Tag found (img to http://img.yahoo.co.kr/images/bull2.gif) => http://img.yahoo.co.kr/images/bull2.gif
Getting http://img.yahoo.co.kr/images/bull2.gif ...
+ Tag found (img to http://img.yahoo.co.kr/images/bull2.gif) => http://img.yahoo.co.kr/images/bull2.gif
+ Tag found (img to http://img.yahoo.co.kr/images/bull2.gif) => http://img.yahoo.co.kr/images/bull2.gif
+ Tag found (img to http://img.yahoo.co.kr/images/bull2.gif) => http://img.yahoo.co.kr/images/bull2.gif
+ Tag found (img to http://img.yahoo.co.kr/images/new3.gif) => http://img.yahoo.co.kr/images/new3.gif
Getting http://img.yahoo.co.kr/images/new3.gif ...
+ Tag found (img to http://img.yahoo.co.kr/images/m5v9.gif) => http://img.yahoo.co.kr/images/m5v9.gif
Getting http://img.yahoo.co.kr/images/m5v9.gif ...
+ Tag found (img to http://img.yahoo.co.kr/images/worldcup/logo.gif) => http://img.yahoo.co.kr/images/worldcup/logo.gif
Getting http://img.yahoo.co.kr/images/worldcup/logo.gif ...
kornet(perky):~/test/urlget>$ find .
.
./urlget.py
./index.html
./images
./images/mani2.gif
./images/bul.gif
./images/shop_i.gif
./images/bull2.gif
./images/new3.gif
./images/m5v9.gif
./images/worldcup
./images/worldcup/logo.gif
kornet(perky):~/test/urlget>$

------------------------------------------------------------
>답변해주셔서 감사합니다.

------------------------------------------------------------
>HTML을 얻어온 후 적절한 파싱을 거쳐서 일일이 img태그를
뽑아낸 다음 src를 다시 urlopen해야합니다.

------------------------------------------------------------
>#snow.py

import urllib

ok = urllib.urlopen("http://www.snowcat.co.kr/diary/0109/0915.html")

print ok.read()

이걸 웹상에서 브라우져로 실행을 시켰거든요.

그런데 그림을 못가져오네요..

그림을 가져오려면 어떻게 해야지요?
위로
   
이전 글 표시:   
글 쓰기   답변 달기    파이썬 마을 게시판 인덱스 -> 파이썬 질문과 답변 시간대: GMT + 9 시간(한국)
페이지 11

 
건너뛰기:  
새로운 주제를 올릴 수 없습니다
답글을 올릴 수 없습니다
주제를 수정할 수 없습니다
올린 글을 삭제할 수 없습니다
투표를 할 수 없습니다



Powered by phpBB © 2001, 2005 phpBB Group
회선/장비: Daum DNA , 관리: 장혜식,서상현