2016-05-12 13 views
0

このサイトのすべての画像を取得するにはどうすればいいですか:http://www.theft-alerts.com 19ページの画像が必要です。私たちはまだこのコードを持っていますが、まだ動作しません。私たちは新しい地図の中にその画像を望みます。ウェブサイトから画像をスクラップする方法は?

import requests 

from bs4 import BeautifulSoup 
from urlparse import urljoin 

def get_pages(start): 
    soup = BeautifulSoup(requests.get(start).content) 
    images = [img["src"] for img in soup.select("div.itemspacingmodified a img")] 
    yield images 
    nxt = soup.select("code.resultnav a")[-1] 
    while True: 
     soup = BeautifulSoup(requests.get(urljoin(url, nxt["href"])).content) 
     nxt = soup.select("code.resultnav a")[-1] 
     if nxt.text != "Next": 
      break 
     yield [img["src"] for img in soup.select("div.itemspacingmodified a img")] 




url = "http://www.theft-alerts.com/" 

for images in get_pages(url): 
    print(images) 

あなたを与える:

#!/usr/bin/python 

import [urllib2][1] 
from bs4 import BeautifulSoup 
from urlparse import urljoin 

url = "http://www.theft-alerts.com/index-%d.html" 
page = urllib2.urlopen(url).read() 
soup = BeautifulSoup(page, "html.parser") 

base = "http://www.theft-alerts.com" 

images = [urljoin(base,a["href"]) for a in soup.select("td a[href^=images/]")] 

for url in images: 
    img = BeautifulSoup(urllib2.urlopen(url).read(),"lxml").find("img")["src"] 
with open("myimages/{}".format(img), "w") as f: 
    f.write(urllib2.urlopen("{}/{}".format(url.rsplit("/", 1)[0], img)).read()) 
+2

「まだ動作しません」なぜ表示されますか?最低限、あなたのURLには、まだ記入していないパラメータが含まれています。 –

答えて

0

はテキスト"Next"とアンカーがクラスresultnavとコードタグになるまで、あなたがループし続けることができ、すべてのページをループする必要があり、画像を抽出しますすべての19ページからの画像。