亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何每六分鐘從網站下載一張圖像?

如何每六分鐘從網站下載一張圖像?

慕妹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 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號