我想使用Selenium Python从google搜索中获取前10幅图像的url

我想使用Selenium Python从google搜索中获取前10幅图像的url,python,selenium,Python,Selenium,我想从谷歌搜索(不是base64)中获得前10幅图像的url。 我有密码: import os import base64 import time from selenium.webdriver.common.keys import Keys from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager searchterm = 'bananas' # will also be

我想从谷歌搜索(不是base64)中获得前10幅图像的url。 我有密码:

import os
import base64
import time

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

searchterm = 'bananas'  # will also be the name of the folder
url = "https://www.google.com/search?q=banan&source=lnms&tbm=isch&sa=X&ved=2ahUKEwj-75rDlJLoAhWLHHcKHStFC6EQ_AUoAXoECA4QAw&biw=1867&bih=951"
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
browser.get(url)
actions = webdriver.common.action_chains.ActionChains(browser)
header = {
    'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}
counter = 0
succounter = 0

if not os.path.exists(searchterm):
    os.mkdir(searchterm)

for i in range(0, 11):
    time.sleep(1)
    x = browser.find_elements_by_xpath('//*[@id="islrg"]/descendant::img')[i]
    x.click()
    i += 1
    if i > 10:
        break
    ba = browser.find_element_by_xpath('//* 
    [@id="Sva75c"]/div/div/div[3]/div[2]/div/div[1]/div[1]/div/div[2]/a/img')
    print(ba.get_attribute('src'))
它返回图像URL,但有时返回base64。如何使脚本始终返回图像url?
谢谢。

更改xpath以获取链接而不是图像,然后获取href

ba = browser.find_element_by_xpath("//div[@class='islrc']//a[@href][@rel='noopener']")
print(ba.get_attribute("href")

您可以尝试使用以下答案检查url字符串是否包含base64内容: