当我从google抓取图像时,image src包含非

2024-05-18 23:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在我开始提问之前,我很抱歉我是一个韩国高中生,所以我的问题很难理解。你知道吗

我想我的代码打印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无

我在谷歌上搜索了大约一个小时,但找不到错误(bug?)关于这个 这就是为什么我第一次在stackoverflow上提问

我跳过了名为make\u 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,直到我23岁,但当它超过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无


Tags: importbrowsersrccomimgcountkeywordmax
1条回答
网友
1楼 · 发布于 2024-05-18 23:30:54

尝试将此作为爬网功能。Google使用延迟加载,这会导致图像链接成为属性data src的值,直到图像进入视口。我还没有测试这个片段,但应该可以

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',只是为了表示的目的

相关问题 更多 >

    热门问题