Python中文
首页
教程
问答
标签
搜索
登录
注册
无法使用Python从url链接下载图像
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我想从网上下载一个图像,但是不管使用什么方法,我总是得到一个错误。但是,我可以使用这种方法下载其他图像。在</p> <p>作品-<a href="https://mobec.sk/proxy/geo?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng8&TRANSPARENT=true&layers=global%3Abase&tiled=true&STYLES=&WIDTH=512&HEIGHT=512&CRS=EPSG%3A3857&FORMAT_OPTIONS=dpi%3A180&BBOX=1934774.0599543825%2C6239707.492975509%2C1937220.044859508%2C6242153.477880634" rel="nofollow noreferrer">https://mobec.sk/proxy/geo?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng8&TRANSPARENT=true&layers=global%3Abase&tiled=true&STYLES=&WIDTH=512&HEIGHT=512&CRS=EPSG%3A3857&FORMAT_OPTIONS=dpi%3A180&BBOX=1934774.0599543825%2C6239707.492975509%2C1937220.044859508%2C6242153.477880634</a></p> <p>不起作用-<a href="https://mobec.sk/proxy/geo?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&layers=custom%3Avrbovce_elektrina&tiled=true&STYLES=&WIDTH=512&HEIGHT=512&CRS=EPSG%3A3857&FORMAT_OPTIONS=dpi%3A180&BBOX=1947003.9844800085%2C6242153.477880634%2C1949449.969385134%2C6244599.46278576" rel="nofollow noreferrer">https://mobec.sk/proxy/geo?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&layers=custom%3Avrbovce_elektrina&tiled=true&STYLES=&WIDTH=512&HEIGHT=512&CRS=EPSG%3A3857&FORMAT_OPTIONS=dpi%3A180&BBOX=1947003.9844800085%2C6242153.477880634%2C1949449.969385134%2C6244599.46278576</a></p> <p><strong>方法1</strong></p> <p>使用Python的请求和枕头库。在这里,图像应该直接从url下载。在</p> <pre><code>import requests from PIL import Image from io import BytesIO url = 'https://mobec.sk/proxy/geo?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&layers=custom%3Avrbovce_elektrina&tiled=true&STYLES=&WIDTH=512&HEIGHT=512&CRS=EPSG%3A3857&FORMAT_OPTIONS=dpi%3A180&BBOX=1947003.9844800085%2C6242153.477880634%2C1949449.969385134%2C6244599.46278576' response = requests.get(url) img = Image.open(BytesIO(response.content)) # error occurs here img.save('output.png') </code></pre> <p>我得到以下错误:</p> ^{pr2}$ <p><strong>方法2</strong></p> <p>使用Python的Selenium库和chromedriver。这里我尝试在web浏览器中显示图像,并使用Selenium库打印整个页面。在</p> <pre><code>from selenium import webdriver from PIL import Image from io import BytesIO url = 'https://mobec.sk/proxy/geo?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&layers=custom%3Avrbovce_elektrina&tiled=true&STYLES=&WIDTH=512&HEIGHT=512&CRS=EPSG%3A3857&FORMAT_OPTIONS=dpi%3A180&BBOX=1947003.9844800085%2C6242153.477880634%2C1949449.969385134%2C6244599.46278576' chrome = webdriver.Chrome('path/to/chromedriver') chrome.get(url) png = chrome.get_screenshot_as_png() im = Image.open(BytesIO(png)) im.save('output.png') chrome.quit() </code></pre> <p>这种方法的问题是,在chromedriver驱动程序打开的Chrome浏览器中没有加载(显示)图像。当手动将链接粘贴到普通(桌面应用)Chrome中时,会显示图像。在</p> <p><a href="https://i.stack.imgur.com/Chf5X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Chf5X.png" alt="Method 2 - image does not load into chrome"/></a></p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>使用请求,只需将响应内容写入文件:</p> <pre><code>with open('image-file', 'wb') as fp: fp.write(response.content) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
8 回答
如何添加表示整数的擦边字符串?
2 回答
如何添加要在Bokeh中使用的新font.ttf文件?
10 回答
如何添加要显示的矩阵XY轴编号和XY轴
10 回答
如何添加计数?
7 回答
如何添加计数器函数?
4 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
4 回答
如何添加计数器来跟踪while循环中的月份和年份?
7 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
3 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
1 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
8 回答
如何添加评论拉梅尔亚姆
4 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
4 回答
如何添加谷歌地点自动完成到Flask?
9 回答
如何添加超时、python discord bot
2 回答
如何添加超过1dp的检查
9 回答
如何添加距离方法
9 回答
如何添加跟随游戏的敌人精灵
7 回答
如何添加路径以便python可以找到程序?
2 回答
如何添加身份验证/安全性以使用happybase访问HBase?
6 回答