【问题标题】:How to scrape the images from the website?如何从网站上抓取图像?
【发布时间】:2016-09-08 03:01:30
【问题描述】:

如何获取本站所有图片:http://www.theft-alerts.com 我们需要 19 页的图像。到目前为止,我们有这个代码,但它还不能工作。我们想要新地图中的图像。

#!/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())

【问题讨论】:

  • “还不行” 你明白为什么吗?至少,你的 url 包含一个你还没有填写的参数。

标签: python json image web-scraping beautifulsoup


【解决方案1】:

使用 Python 进行图像抓取

此代码肯定适用于抓取 Google 图片。

import os
import time
import requests
from selenium import webdriver
def fetch_image_urls(query: str, max_links_to_fetch: int, wd: webdriver, 
sleep_between_interactions: int = 1):
    def scroll_to_end(wd):
        wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(sleep_between_interactions)

    # build the google query

search_url = "https://www.google.com/search?safe=off&site=&tbm=isch&source=hp&q= 
{q}&oq={q}&gs_l=img"

# load the page
wd.get(search_url.format(q=query))

image_urls = set()
image_count = 0
results_start = 0
while image_count < max_links_to_fetch:
    scroll_to_end(wd)

    # get all image thumbnail results
    thumbnail_results = wd.find_elements_by_css_selector("img.Q4LuWd")
    number_results = len(thumbnail_results)

    print(f"Found: {number_results} search results. Extracting links from 
{results_start}:{number_results}")

    for img in thumbnail_results[results_start:number_results]:
        # try to click every thumbnail such that we can get the real image behind it
        try:
            img.click()
            time.sleep(sleep_between_interactions)
        except Exception:
            continue

        # extract image urls
        actual_images = wd.find_elements_by_css_selector('img.n3VNCb')
        for actual_image in actual_images:
            if actual_image.get_attribute('src') and 'http' in 
actual_image.get_attribute('src'):
                image_urls.add(actual_image.get_attribute('src'))

        image_count = len(image_urls)

        if len(image_urls) >= max_links_to_fetch:
            print(f"Found: {len(image_urls)} image links, done!")
            break
    else:
        print("Found:", len(image_urls), "image links, looking for more ...")
        time.sleep(30)
        return
        load_more_button = wd.find_element_by_css_selector(".mye4qd")
        if load_more_button:
            wd.execute_script("document.querySelector('.mye4qd').click();")

    # move the result startpoint further down
    results_start = len(thumbnail_results)

return image_urls


def persist_image(folder_path:str,url:str, counter):
    try:
        image_content = requests.get(url).content

    except Exception as e:
        print(f"ERROR - Could not download {url} - {e}")

    try:
        f = open(os.path.join(folder_path, 'jpg' + "_" + str(counter) + ".jpg"), 'wb')
        f.write(image_content)
        f.close()
        print(f"SUCCESS - saved {url} - as {folder_path}")
    except Exception as e:
        print(f"ERROR - Could not save {url} - {e}")

def search_and_download(search_term: str, driver_path: str, target_path='./images', 
number_images=10):
    target_folder = os.path.join(target_path, '_'.join(search_term.lower().split(' 
')))

    if not os.path.exists(target_folder):
        os.makedirs(target_folder)

    with webdriver.Chrome(executable_path=driver_path) as wd:
        res = fetch_image_urls(search_term, number_images, wd=wd, 
sleep_between_interactions=0.5)

    counter = 0
    for elem in res:
        persist_image(target_folder, elem, counter)
        counter += 1

# How to execute this code
# Step 1 : pip install selenium. pillow, requests
# Step 2 : make sure you have chrome installed on your machine
# Step 3 : Check your chrome version ( go to three dot then help then about google 
chrome )
# Step 4 : Download the same chrome driver from here  " 
https://chromedriver.storage.googleapis.com/index.html "
# Step 5 : put it inside the same folder of this code

DRIVER_PATH = './chromedriver'
search_term = 'iphone'
# num of images you can pass it from here  by default it's 10 if you are not passing
#number_images = 10
search_and_download(search_term=search_term, driver_path=DRIVER_PATH)

【讨论】:

    【解决方案2】:

    您需要遍历每个页面并提取图像,您可以一直循环直到带有文本"Next" 的锚点位于带有类resultnav 的代码标记中:

    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)
    

    它会为您提供所有 19 页的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2022-10-14
      • 2017-02-13
      • 2021-10-25
      相关资源
      最近更新 更多