[英]How to capture website screenshot in high resolution?
我想以高分辨率捕获网站的屏幕截图以识别文本或仅保存高质量的图像。 我在 Python 2.7 中尝试了这段代码。 网站http:\/\/www.flaticon.com\/<\/a>仅作为示例。
from selenium import webdriver
import time
driver = webdriver.PhantomJS()
#Setting large window size doesn`t resolve the problem
driver.set_window_size(16000, 12000)
driver.get('http://www.flaticon.com/')
time.sleep(3)
#set resolution 640 dots per inch for this image
#???
driver.save_screenshot('./downloaded/img/welcome_icons.png') # save a screenshot to disk
driver.close()
这有点hacky,但我通过将窗口大小宽度增加到3000并将缩放比例增加到250%为自己解决了这个问题。
driver.set_window_size(3000,800)
driver.execute_script("document.body.style.zoom='250%'")
希望这可以帮助。
我遇到了完全相同的问题,使用 Python 3.7 和 chrome webdriver,上面的答案有很大帮助,但没有一个对我来说很完美。 这是我找到的解决方案:
from selenium import webdriver
from PIL import Image
from io import BytesIO
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# MUST BE HEADLESS AND HAVE VERY LARGE WINDOW SIZE
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=6000x5000")
def get_screenshot(url_path, object_type, object_id):
chrome = webdriver.Chrome(chrome_options=chrome_options,
executable_path='/path/to/webdriver')
chrome.get('http://website.com' + url_path)
chrome.execute_script("document.body.style.zoom = '300%'") # ZOOM
element = chrome.find_element_by_id(object_id) # find part of the page you want image of
location = element.location
size = element.size
png = chrome.get_screenshot_as_png() # saves screenshot of entire page
chrome.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x'] * 3 # must mutliply all these numbers by your zoom
top = location['y'] * 3
right = (location['x'] + size['width']) * 3
bottom = (location['y'] + size['height']) * 3
im = im.crop((left, top, right, bottom)) # defines crop points
im.save(f'{object_id}.png') # saves new cropped image
如果是关于改变窗口大小,你可以通过
driver.set_window_size(480, 320)
这是其中一位开发人员在Github上执行此操作的示例。 如您所见,您可以调整屏幕截图的窗口大小和质量。
import StringIO
from selenium import webdriver
from PIL import Image
# Install instructions
#
# npm install phantomjs
# sudo apt-get install libjpeg-dev
# pip install selenium pillow
driver = webdriver.PhantomJS(executable_path="node_modules/phantomjs/bin/phantomjs")
driver.set_window_size(1366, 728) # optional
driver.get('http://google.com')
driver.save_screenshot('screen_hires.png')
screen = driver.get_screenshot_as_png()
# Crop it back to the window size (it may be taller)
box = (0, 0, 1366, 728)
im = Image.open(StringIO.StringIO(screen))
region = im.crop(box)
region.save('screen_lores.jpg', 'JPEG', optimize=True, quality=95)
100 的质量是最大值,0 - 最小值。
编辑:
您还可以使用selenium.windowMaxmize()
。
如果您想放大屏幕以查看您所说的某些特定文本,您可以在 Mozilla 中尝试:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
br = webdriver.Firefox()
zoom = ActionChains(br)
body = br.find_element_by_tag_name('body')
for i in range(2):
zoom.send_keys_to_element(body,Keys.CONTROL,"+").perform()
要在 selenium 中获取更高 DPI 的屏幕截图,您需要做的就是在您的驱动程序配置文件中设置单行首选项。 让我们对 Firefox 和 Chrome 都这样做:
铬合金<\/h2>desired_dpi = 2.0 options = ChromeOptions() options.add_argument(f"--force-device-scale-factor={desired_dpi}") driver = webdriver.Chrome(chrome_options=options)<\/code><\/pre>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.