2013-01-01 2 views
2

가능한 중복 :
How to download image using requests긁힌 URL 목록에서 이미지를 다운로드하는 방법은 무엇입니까?

나는 텀블러 블로그의 이미지 URL을 긁어이 파이썬 스크립트를 가지고 있고, 내 바탕 화면의 로컬 폴더에 다운로드하고 싶습니다. 나는이

import requests 
from bs4 import BeautifulSoup 

def make_soup(url): 
#downloads a page with requests and creates a beautifulsoup object 

    raw_page = requests.get(url).text 
    soup = BeautifulSoup(raw_page) 

    return soup 


def get_images(soup): 
#pulls images from the current page 

    images = [] 

    foundimages = soup.find_all('img') 

    for image in foundimages: 
     url = img['src'] 

     if 'media.tumblr.com' in url: 
      images.append(url) 


    return images 


def scrape_blog(url): 
# scrapes the entire blog 

    soup = make_soup(url) 

    next_page = soup.find('a' id = 'nextpage') 

    while next_page is not none: 

     soup = make_soup(url + next_page['href']) 
     next_page = soup.find('a' id = 'nextpage') 

     more_images = get_images(soup) 
     images.extend(more_images) 

    return images 


url = 'http://x.tumblr.com' 
images = scrape_blog(url) 

답변

1

파이썬의 "urllib2"을 구현하는 방법에 대한 갈 것이라고 어떻게 당신이 찾고있는 무엇을 아마. 쿠키 나 인증과 같이 복잡한 작업이 필요한 경우 Requests과 같은 래퍼 라이브러리를 살펴 보는 것이 좋습니다.이 라이브러리는 표준 라이브러리의 더 복잡한 기능에 대한 멋진 래퍼를 제공합니다.

관련 문제