【问题标题】:When I crawl image from google, image src includes None当我从谷歌抓取图像时,图像 src 包含无
【发布时间】:2019-09-22 00:02:58
【问题描述】:

在我开始提问之前,我很抱歉我是一名韩国高中生,所以我的问题可能难以阅读。

我希望我的代码打印图像的 src,但是当我超过 22 岁时它会打印无,所以我无法下载任意数量的图像。

它是这样打印的。 这是我插入关键字“猫”时的图像 src。

20https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQdMIU_4V4XtUAiV2uOBmeixkhQuy6N3eaHH1XuUzOYFyQZBZefEg

21https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQvmdG435HxyF0e1DP1IBVos10zTwuNJ0p9M_iYDzlYWup6AgfV6w

22https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQL8NCMT9h7p8koWq3pgyhS8EebE9qh24e-5SQWzIpmDgBNvNaO

23 无

24 无

25 无

26 无

我在谷歌上搜索了大约一个小时,但我找不到这个错误(错误?) 这就是我第一次在stackoverflow上提出问题的原因

我跳过了名为 make_dir 的函数

import os
import shutil
import urllib.request
import time

from selenium import webdriver

def crawl(keyword, max_count):
    cnt = 0

    url = "https://www.google.co.in/search?q=" + keyword + "&tbm=isch"  # google search url with search word

    browser = webdriver.Chrome("C:\\Users\\Master\\Desktop\\crawling\\chromedriver.exe")  # webdriver
    browser.get(url)  # open web page

    img_list = browser.find_elements_by_class_name("rg_ic")  # find image


    for i, el in enumerate(img_list):
        if cnt >= max_count:
            break

        img = img_list[i]
        src = img.get_attribute('src')
        if src is None:
            print(i, src)  # img_list includes None so I need to fix it
            continue

        cnt += 1
        print(i, src)  # print src
        urllib.request.urlretrieve(src, str(cnt) + ".png")  # download image

    browser.quit()

if __name__ == "__main__":
    max_count = int(input("Number of crawls : "))
    keyword = input("Search word : ")

    make_dir()
    crawl(keyword, max_count)

我编写了打印 src 的代码。 它打印 src 直到 i 是 23,但是当它超过 22 时,这些只打印 None 我想让它们打印正确的 src

20https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQdMIU_4V4XtUAiV2uOBmeixkhQuy6N3eaHH1XuUzOYFyQZBZefEg

21https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQvmdG435HxyF0e1DP1IBVos10zTwuNJ0p9M_iYDzlYWup6AgfV6w

22https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQL8NCMT9h7p8koWq3pgyhS8EebE9qh24e-5SQWzIpmDgBNvNaO

23 无

24 无

25 无

26 无

【问题讨论】:

  • 我没有亲自测试过,但您可能会遇到这个问题,因为谷歌使用延迟加载来加载图像,这意味着只加载视口中的图像。您可能需要滚动才能加载这些图像。请参阅stackoverflow.com/questions/20986631/…的答案
  • 很高兴您能帮助我,但我认为您指出的问题与本案无关。我试过你指出,因为它可以工作,但这仍然包括列表中的 None
  • 嘿,检查我的答案,如果它有效,请告诉我

标签: python selenium


【解决方案1】:

试试这个作为你的抓取功能。 Google 使用延迟加载,这会导致图像链接成为属性 data-src 的值,直到图像进入视口。我还没有测试过 sn-p 但它应该可以工作

def crawl(keyword, max_count):
    cnt = 0

    url = "https://www.google.co.in/search?q=" + keyword + "&tbm=isch"  # google search url with search word

    browser = webdriver.Chrome("C:\\Users\\Master\\Desktop\\crawling\\chromedriver.exe")  # webdriver
    browser.get(url)  # open web page

    img_list = browser.find_elements_by_class_name("rg_ic")  # find image


    for i, el in enumerate(img_list):
        if cnt >= max_count:
            break

        img = img_list[i]
        src = img.get_attribute('src')
        if src is None:
            src = img.get_attribute('data-src')
            if src is None:
                continue


        cnt += 1
        print(i, src)  # print src
        if src[0]=='h':
            urllib.request.urlretrieve(src, str(cnt) + ".png")
        else:
            with open(str(cnt) + ".png", "wb") as fh:

                print(src[23:])
                fh.write(base64.b64decode(src[22:]))

    browser.quit()

该代码使用了一些丑陋的技巧,例如 if src[0]=='h' 并且仅用于表示目的

【讨论】:

  • 非常感谢您对我的帮助,但是当我使用您的代码时,出现了一些错误
  • 很高兴为您提供帮助,请问您遇到了什么错误?我检查了我机器上的 sn-p,它正确检索了所有 URL
  • Traceback(最近一次调用最后):文件“C:/Users/master/Desktop/crawling/crawling.py”,第 58 行,在 crawl(keyword, max_count) 文件“C :/Users/master/Desktop/crawling/crawling.py", line 33, in crawl urllib.request.urlretrieve(src, str(cnt) + ".png") # 下载图片文件 "C:\Users\master\ AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 245 行,在 urlretrieve url_type 中,路径 = splittype(url) 文件“C:\Users\master\AppData\Local\Programs\Python\Python37 \lib\urllib\parse.py",第 973 行,在 splittype 中
  • match = _typeprog.match(url) TypeError: expected string or bytes-like object
  • 这是因为 Google 将可见图像编码为 base64,导致图像源是图像本身,而不是有效的 URL。您可以使用简单的条件来检查“src”是否为有效 URL,如果不是,则将其视为 base64 编码图像并将其写入文件
猜你喜欢
  • 2017-09-05
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多