0

我正在尝试使用 Splinter 浏览器库(带有 ChromeDriver)和 Pillow 库的组合从网页下载图像。我正在使用 Chrome 浏览器的实例登录网站并导航到带有图像的页面,然后使用该browser.find_by_tag()方法定位图像。

然后我尝试使用Image.save()Pillow 的方法,但它抛出了这个错误:

C:\Users\my.name\Documents\Development\Python-Scripts>my_script.py
Traceback (most recent call last):
  File "C:\Users\my.name\Documents\Development\Python-Scripts\my_script.p
y", line 31, in <module>
    my_function()
  File "C:\Users\my.name\Documents\Development\Python-Scripts\my_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()
4

1 回答 1

2

effbot 示例中,您以错误的方式称呼它。代替

    farmer_image_brobj = browser.find_by_tag('img')
    farmer_image = Image.save(farmer_image_brobj)

您应该save()在 Image 的实例上使用该方法。所以首先,打开图片:

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')
于 2014-10-15T09:37:52.400 回答