我正在尝试使用 python 检索通过给定 URL 返回的图像,例如这个:
http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108
我尝试使用 urllib 检索方法来执行此操作:
import urllib
urlStr = "http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108"
filename = "image.png"
urllib.urlretrieve(urlStr,filename)
我已经将其用于其他 URL(例如 http://chart.finance.yahoo.com/z?s=CMIG4.SA&t=9m ),但对于第一个 URL 不起作用。
有人知道如何为给定的 URL 制作此内容吗? 注意:我使用的是 Python 2.7
最佳答案
您需要使用一个可以使用 requests 执行的 session :
import requests
with requests.Session() as s:
s.get("http://fundamentus.com.br/graficos.php?papel=CMIG4&tipo=2")
with open("out.png", "wb") as f:
f.write(s.get("http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108").content)
它可以在您的浏览器中运行,因为您访问了图像所在的初始页面,因此设置了所有必要的 cookie。
关于python - 从Python中的URL调用的方法中检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39008074/