我正试图使用Splinter浏览器库(带ChromeDriver)和Pillow库的组合从网页下载图像。我使用Chrome浏览器的一个实例登录到网站并导航到带有图像的页面,然后使用browser.find_by_tag()
方法定位图像。
然后我尝试使用Pillow的Image.save()
方法,但它抛出了以下错误:
C:Usersmy.nameDocumentsDevelopmentPython-Scripts>my_script.py
Traceback (most recent call last):
File "C:Usersmy.nameDocumentsDevelopmentPython-Scriptsmy_script.p
y", line 31, in <module>
my_function()
File "C:Usersmy.nameDocumentsDevelopmentPython-Scriptsmy_script.p
y", line 19, in my_function
farmer_image = Image.save(farmer_image_brobj)
AttributeError: 'module' object has no attribute 'save'
我的代码:
from splinter import Browser
from PIL import Image
def my_function():
with Browser('chrome') as browser:
url = "http://www.blah/login.aspx"
url_two = "http://www.blah/ImagePageParent.aspx"
browser.visit(url)
browser.fill('un', 'blah')
browser.fill('pw', 'blah')
browser.find_by_name('btn').click()
browser.visit(url_two)
browser.find_link_by_partial_href('partial_href').click()
farmer_image_brobj = browser.find_by_tag('img')
farmer_image = Image.save(farmer_image_brobj)
browser.visit(url_two)
browser.quit()
my_function()
从effbot示例来看,您的调用方式不对。代替
farmer_image_brobj = browser.find_by_tag('img')
farmer_image = Image.save(farmer_image_brobj)
您应该在Image的实例上使用save()
方法。因此,首先打开图像:
farmer_imag = Image.open(...) #something doing with farmer_image_brobj
例如,使用请求,您可以执行以下操作:
r = requests.get('http://example.com/image.jpg')
farmer_image = Image.open(StringIO(r.content))
然后通过提供路径保存:
farmer_image.save('my_image.png')