Created
May 15, 2021 13:53
-
-
Save jeremy-rutman/657009bcaaaf376bd4ace5f5b4b23051 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#assumes selenium browser object | |
def get_img_and_further_info(browser,img_src): | |
try: | |
im = imageio.imread(img_src) | |
plt.plot(im) | |
plt.imshow() | |
# vv.imshow(im) | |
imageio.imwrite('test.jpg',im) | |
except Exception as e: | |
print('trouble getting img with imgeio',str(e)) | |
try: | |
local = 'test.jpg' | |
urllib.request.urlretrieve(img_src, local) | |
except Exception as e: | |
print('troubl egetting img with urllib',str(e)) | |
try: | |
r = requests.get(img_src, stream=True,headers={'User-agent': 'Mozilla/5.0'}) | |
if r.status_code == 200: | |
with open("img.png", 'wb') as f: | |
r.raw.decode_content = True | |
# shutil.copyfileobj(r.raw, f) | |
except Exception as e: | |
print('trb with requests 1 '+str(e)) | |
try: | |
res = requests.get(img_src).content | |
if len(res)>0: | |
print('got img') | |
except Exception as e: | |
print('trb with requests 2'+str(e)) | |
try: | |
with urllib.request.urlopen(img_src) as response: | |
with tempfile.NamedTemporaryFile(delete=False) as tmp_file: | |
shutil.copyfileobj(response, tmp_file) | |
except Exception as e: | |
print('trb with urllib' + str(e)) | |
try: | |
opener = urllib.request.build_opener() | |
opener.addheaders = [('User-Agent', | |
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')] | |
urllib.request.install_opener(opener) | |
local = 'test.jpg' | |
urllib.request.urlretrieve(img_src, local) | |
except Exception as e: | |
print('trb with urllib'+str(e)) | |
try: | |
# proxy = ProxyHandler({'http': 'http://192.168.1.31:8888'}) | |
img_xpath = '/html/body/div[4]/div[5]/div[3]/div[11]/img' | |
img_field = browser.find_element_by_xpath(img_xpath) | |
img_data = img_field.screenshot_as_png | |
img_data_b64 = img_field.screenshot_as_base64 | |
img_data_b64_decoded = base64.b64decode(img_data_b64) | |
with open('test.jpg','wb') as fp: | |
fp.write(img_data_b64_decoded) | |
except Exception as e: | |
print('trouble getting img using screenshote'+str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment