问题描述
我正在尝试使用Python中的Selenium Webdriver从URL下载图像.该站点受登录页面保护,因此不能仅使用请求保存URL内容.登录后我可以从网站上获取文本,但是我不知道如何保存图像.
I am trying to download an image from a URL using Selenium Webdriver in Python. The site is protected by a login page, so can't just save the URL contents using requests. I am able to get text from the site after logging in, but I can't figure out how to save an image.
登录网站后,我可以执行browser.save_screenshot(filename + '.png')
,但是该图像的大小与原始图像不正确.
After I log in to the site, I can do browser.save_screenshot(filename + '.png')
but that image is not the correct size as the original.
我现在拥有的代码是:
browser = webdriver.Chrome('../chromedriver')
browser.get('www.example.com/login')
# send username and password, click submit
browser.get('www.example.com/123')
html = browser.page_source
printData(html)
# this url is an image file
browser.get('www.example.com/get_photo.php?id=123')
browser.save_screenshot(filename + '.png')
理想情况下,我想将save_screenshot()
替换为
Ideally I would like to replace the save_screenshot()
with something like
with open(filename + '.jpeg', 'w') as img:
image.write(browser.download_current_image())
甚至是类似的东西,与弹出菜单进行交互
or even something like this, interacting with the popup menu
browser.right_click()
browser.down_arrow_key()
browser.return_key()
或模拟按键
browser.command_key()
browser.s_key()
此问题给出了我的答案想要,但不适合Python.如果有一种方法可以用Python处理该问题中建议的任何事情(除了拍摄屏幕截图),那将是一个很好的解决方案.
This question gives the answers that I want, but not for Python. If there is a way to do any of the things suggested in that question (besides taking a screenshot) in Python, that would be a great solution.
推荐答案
在这里,我曾使用Selenium Webdriver登录并从登录页面后面的URL下载图像,然后将cookie传递给请求以保存图像:
Here is what I used to download an image from a URL behind a login page by logging in using Selenium Webdriver and then passing the cookies to requests to save the image:
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
}
s = requests.session()
s.headers.update(headers)
for cookie in browser.get_cookies():
c = {cookie['name']: cookie['value']}
s.cookies.update(c)
r = s.get(imgurl, allow_redirects=True)
open(filename + '.jpeg', 'wb').write(r.content)
感谢 AldoSuwandi 向我展示了如何在帖子之间.我还使用了此网站来帮助我弄清楚如何使用请求下载图像.
Thanks to AldoSuwandi for showing me how to do this in this post. I also used this site to help me figure out how to download an image using requests.
这篇关于在Python中使用Selenium Webdriver下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!