为了账号安全,请及时绑定邮箱和手机立即绑定

如何每六分钟从网站下载一张图像?

如何每六分钟从网站下载一张图像?

慕妹3146593 2023-09-19 14:19:34
我正在开发一个机器学习项目,需要大量图片作为数据集来训练我的程序。这是我到目前为止所拥有的:driver.get('https://its.txdot.gov/ITS_WEB/FrontEnd/default.html?r=SAT&p=San%20Antonio&t=cctv')time.sleep(5) #to let the site loaddriver.find_element_by_id('LP-1604').click() #to get to the 1604 tabtime.sleep(5) #to let the site loadpic = driver.find_element_by_id('LP 1604 at Kyle Seale Pkwy__SAT')action = ActionChains(driver)action.context_click(pic)右键单击时通常会弹出的下拉菜单不显示。我觉得必须有一种比右键单击更好的方法来做到这一点。我知道如何将其包装在每六分钟执行一次的循环中,因此我不需要那里的帮助。这只是下载图像部分。我遇到的问题之一是所有图像都在同一个 url 下,并且大多数示例都使用 url。任何的意见都将会有帮助。
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

我认为它可以帮助您将图像保存在电脑中:


from PIL import Image


def save_image_on_disk(driver, element, path):

    location = element.location

    size = element.size

    # saves screenshot of entire page

    driver.save_screenshot(path)


    # uses PIL library to open image in memory

    image = Image.open(path)


    left = location['x']

    top = location['y'] + 0

    right = location['x'] + size['width']

    bottom = location['y'] + size['height'] + 0


    image = image.crop((left, top, right, bottom))  # defines crop points

    image = image.convert('RGB')

    image.save(path, 'png')  # saves new cropped image

def your_main_method():

    some_element_img = driver.find_element_by_xpath('//*[@id="id-of-image"]')

    save_image_on_disk(driver, some_element_img, 'my-image.png')

关于你应该使用 time.sleep(6*60) 的时间


查看完整回答
反对 回复 2023-09-19
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

图像数据位于 currentSnap 元素的 src 属性中。它以 Base64 编码,因此您需要捕获它并解码它。然后使用 PIL,您可以对图像执行任何您喜欢的操作。


您还可以使用 selenium 的内置等待函数而不是硬编码睡眠。在这种情况下,有时即使在图像元素加载之后也会加载图像,因此代码中仍然有一个额外的短暂睡眠以允许其加载。


from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC



from PIL import Image

from io import BytesIO

import base64

import re


# Max time to wait for page to load

timeout=10


driver = webdriver.Chrome()

driver.get('https://its.txdot.gov/ITS_WEB/FrontEnd/default.html?r=SAT&p=San%20Antonio&t=cctv')


# Wait for element to load before clicking

element_present = EC.presence_of_element_located((By.ID, 'LP-1604'))

WebDriverWait(driver, timeout).until(element_present)


driver.find_element_by_id('LP-1604').click() #to get to the 1604 tab


# Waat for image to load before capturing data

element_present = EC.presence_of_element_located((By.ID, 'currentSnap'))

WebDriverWait(driver, timeout).until(element_present)


# Sometimes the image still loads after the element is present, give it a few more seconds

time.sleep(4)

# Get base64 encoded image data from src

pic = driver.find_element_by_id('currentSnap').get_attribute('src')


# Strip prefix

pic = re.sub('^data:image/.+;base64,', '', pic)


# Load image file to memory

im = Image.open(BytesIO(base64.b64decode(pic)))


# Write to disk

im.save('image.jpg')


# Display image in Jupyter

im

# Open in your default image viewer

im.show()


查看完整回答
反对 回复 2023-09-19
  • 2 回答
  • 0 关注
  • 52 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信