我得到的這段代碼大部分都在作業,只是保存的影像并不足夠。我的 x、y、寬度、高度截取了與影像不匹配的螢屏截圖。我認為有更好的方法可以做到這一點,但我不知道如何。任何幫助表示贊賞。
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from PIL import Image
import time
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://superrare.com/features/rare-steals-1")
time.sleep(2)
imgsrc2 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(@class,'slide selected')]")))
#the element with longest height on page
ele=driver.find_element("xpath", '//div[@id="root"]')
total_height = ele.size["height"] 8000
driver.set_window_size(1920, total_height) #the trick
time.sleep(2)
driver.save_screenshot("screenshot1.png")
for i in imgsrc2:
location = i.location
size = i.size
x = location['x']
y = location['y']
width = x size['width']
height = y size['height']
fullImg = Image.open("screenshot1.png")
cropImg = fullImg.crop((x, y, x width, y height))
cropImg.save("\\images\\new-img" str(i) ".png")
driver.quit()
整個頁面的大螢屏截圖很好,但我得到了像這樣的裁剪影像

uj5u.com熱心網友回復:
下面是使用 firefox 驅動的代碼:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from PIL import Image
import time
driver = webdriver.Firefox()
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://superrare.com/features/the-intersection-of-machine-and-artist")
time.sleep(2)
#the element with longest height on page
ele=driver.find_element("xpath", '//div[@id="root"]')
total_height = ele.size["height"] 12000
time.sleep(2)
driver.set_window_size(2500, total_height) #the trick
time.sleep(2)
driver.save_screenshot("screenshot1.png")
time.sleep(3)
imgsrc = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(@class,'slide selected')]")))
imgsrc2 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(@class,'slide selected')]")))
time.sleep(10)
for i in imgsrc2:
location = i.location
size = i.size
x = location['x']
y = location['y']
width = x size['width']
height = y size['height']
# print(x, y, width, height)
fullImg = Image.open("screenshot1.png")
# cropImg = fullImg.crop((x, y, x width, y height))
cropImg = fullImg.crop((int(x), int(y), int(width), int(height)))
cropImg.save("\\images\\new-img" str(i) ".png")
driver.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411726.html
標籤:
