๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป๊ฐœ๋ฐœ/์…€๋ ˆ๋‹ˆ์›€

์…€๋ ˆ๋‹ˆ์›€ - ์ด๋ฏธ์ง€(img) ๋‹ค์šด๋กœ๋“œ (No urllib.urlretrieve)

by Janger 2023. 2. 28.
728x90
๋ฐ˜์‘ํ˜•

URL ์š”์ฒญ ๋ฐฉ์‹์ธ urllib.urlretrieve์€ src ์ฃผ์†Œ๋กœ ์š”์ฒญ์„ ํ•˜๋Š” ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ์บก์ฑ (์ž๋™๋“ฑ๋ก๋ฐฉ์ง€ ๋ฌธ์ž)์ฒ˜๋Ÿผ ๋งค๋ฒˆ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์ด๋ฏธ์ง€๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด ๋ฌด์šฉ์ง€๋ฌผ์ด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด๋ฏธ ๋ธŒ๋ผ์šฐ์ €์— ๋กœ๋“œ๊ฐ€ ๋œ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. 

 

๋ฐฉ๋ฒ• 1. screenshot_as_png ์‚ฌ์šฉ (์ถ”์ฒœ)
from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://www.webpagetest.org/')

with open('filename.png', 'wb') as file:
	file.write(driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[2]/table[1]/tbody/tr/td[1]/a/div').screenshot_as_png)

 

 

๋ฐฉ๋ฒ• 2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ (PIL ๋ชจ๋“ˆ์˜ Image ํ•จ์ˆ˜๋กœ ์ €์žฅ)
from io import BytesIO
from PIL import Image
from base64 import b64decode

driver.get(url)

# Create a canvas, set it's width and height equal to image's
# Write image to canvas, translate to base64
# Remove metadata prefix
b64img = driver.execute_script(r'''
var img = document.getElementsByTagName("img")[0];
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
''')

# Decode from base64, translate to bytes and write to PIL image
img = Image.open(BytesIO(b64decode(b64img)))
img.save("./filename.png")

 

๋ฐฉ๋ฒ• 3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ (ํŒŒ์ผ ์ž…์ถœ๋ ฅ์œผ๋กœ ์ €์žฅ)
from io import BytesIO
from base64 import b64decode

driver.get(url)

# Create a canvas, set it's width and height equal to image's
# Write image to canvas, translate to base64
# Remove metadata prefix
b64img = driver.execute_script(r'''
var img = document.getElementsByTagName("img")[0];
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
''')

imgdata = base64.b64decode(b64img)
filename = 'filename.png'  # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
    f.write(imgdata)

 

 

์ถœ์ฒ˜: 

https://stackoverflow.com/questions/17361742/download-image-with-selenium-python

 

Download image with selenium python

I want get captcha image from browser. I have got a url of this picture, but the this picture changes each updated time (url is constant). Is there any solution to get picture from browser (like '...

stackoverflow.com

 

https://stackoverflow.com/questions/16214190/how-to-convert-base64-string-to-image

 

How to convert base64 string to image?

I'm converting an image to base64 string and sending it from android device to the server. Now, I need to change that string back to an image and save it in the database. Any help?

stackoverflow.com

 

728x90
๋ฐ˜์‘ํ˜•