2013-08-23 4 views
2

URL에서 모든 이미지를 추출하고 다운로드하려고합니다. 내가beautifulSoup를 사용하여 웹 사이트에서 모든 이미지를 추출하고 다운로드하는 방법은 무엇입니까?

import urllib2 
import re 
from os.path import basename 
from urlparse import urlsplit 

url = "http://filmygyan.in/katrina-kaifs-top-10-cutest-pics-gallery/" 
urlContent = urllib2.urlopen(url).read() 
# HTML image tag: <img src="url" alt="some_text"/> 
imgUrls = re.findall('img .*?src="(.*?)"', urlContent) 

# download all images 
for imgUrl in imgUrls: 
    try: 
     imgData = urllib2.urlopen(imgUrl).read() 
     fileName = basename(urlsplit(imgUrl)[2]) 
     output = open(fileName,'wb') 
     output.write(imgData) 
     output.close() 
    except: 
     pass 

가 난 그냥 "다음"버튼을 클릭하지 않고 모든 이미지를 얻으려면 http://i.share.pho.to/1c9884b1_l.jpeg 이 이미지를보고이 페이지의 이미지를 추출하고 싶지 않은 스크립트를 작성 내가 어떻게 할 수지고 있지 않다 나는 "다음"수업에서 모든 사진을 얻는다.? 내가 findall에서해야 할 일은 무엇인가?

+0

BeautifulSoup를 사용하고 싶지만 진행 방법을 잘 모르시겠습니까? –

+0

예. findall 또는 findnext를 어떻게 사용해야합니까? 위의 스크립트는 해당 URL의 모든 이미지를 가져 오지만 다음 버튼을 클릭하면 오는 슬라이드 쇼의 모든 이미지를 가져오고 싶습니다 (이미지 링크 참조). – user2711817

+0

사용 [wget] (http://stackoverflow.com/questions/4602153/how-do-i-use-wget-to-download-all-images-into-a-single-folder) –

답변

-2

사진 만 원한다면 웹 페이지를 스크래핑하지 않고도 사진을 다운로드 할 수 있습니다. 그것이 당신에게 모든 이미지를 줄 것 같은

http://filmygyan.in/wp-content/gallery/katrina-kaifs-top-10-cutest-pics-gallery/cute1.jpg 
http://filmygyan.in/wp-content/gallery/katrina-kaifs-top-10-cutest-pics-gallery/cute2.jpg 
... 
http://filmygyan.in/wp-content/gallery/katrina-kaifs-top-10-cutest-pics-gallery/cute10.jpg 

그래서 간단한 코드 : BeautifulSoup로와

import os 
import urllib 
import urllib2 


baseUrl = "http://filmygyan.in/wp-content/gallery/katrina-kaifs-top-10-"\ 
     "cutest-pics-gallery/cute%s.jpg" 

for i in range(1,11): 
    url = baseUrl % i 
    urllib.urlretrieve(url, os.path.basename(url)) 

클릭하거나 이미지를 스크랩 다음 페이지로 이동해야 할 것입니다 모두 동일한 URL을 가지고있다. 당신이 하다며 스크랩을 원하는 경우 각 페이지는 개별적으로 해당 페이지에서 모든 이미지를 추출하고 스크립트가 실행되는 디렉토리에 작성해야 다음 shutterset_katrina-kaifs-top-10-cutest-pics-gallery

+0

답변이 맞는 경우 [허용] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – ton1c

+0

으로 표시하지만 스크립트는이 경우 작동하지 않습니다. url이 http://filmygyan.in/tamannah-bhatia-spotted-sizzling-hot-at-tv-channel-launch/ 인 경우 여기에서 url이 sexy112.jpg, sexy117.jpg, sexy12.jpg 사이에서 무작위로 변경되기 때문입니다. 왜냐하면 내가 범위 (1,117)에서 그것도 쓰레기 값을 다운로드하기 때문에. – user2711817

+0

다른 URL을 사용하고 계십니까? 그것은 완전히 다른 질문입니다. 새 URL에서 모든 이미지를 가져와야하는 경우 다른 질문을 엽니 다. 사이트의 모든 페이지에서 작동하는 스크립트를 만들고 싶다면 ** NEW ** 질문에 모든 필수 정보 (예 : 각 페이지의 클래스, ID 또는 태그가 사용 된 것과 같습니다) – ton1c

4

거기 클래스를 사용하여 scrathem하려고합니다.

import re 
import requests 
from bs4 import BeautifulSoup 

site = 'http://pixabay.com' 

response = requests.get(site) 

soup = BeautifulSoup(response.text, 'html.parser') 
img_tags = soup.find_all('img') 

urls = [img['src'] for img in img_tags] 


for url in urls: 
    filename = re.search(r'/([\w_-]+[.](jpg|gif|png))$', url) 
    with open(filename.group(1), 'wb') as f: 
     if 'http' not in url: 
      # sometimes an image source can be relative 
      # if it is provide the base url which also happens 
      # to be the site variable atm. 
      url = '{}{}'.format(site, url) 
     response = requests.get(url) 
     f.write(response.content) 
관련 문제