PacktPublishing / Python-Web-Scraping-Cookbook

Python Web Scraping Cookbook, published by Packt
MIT License
119 stars 95 forks source link

Chapter 04 save image, thumbnail... does not work #2

Closed kanyu closed 3 years ago

kanyu commented 5 years ago

I use Python 3.6.7 |Anaconda custom (64-bit)| Windows 10 x64 07_create_image_thumbnail.py Here is the error

Traceback (most recent call last): File "J:/Scraping/scrape-env/PythonWebScraping/py/04/07_create_image_thumbnail.py", line 3, in from core.file_blob_writer import FileBlobWriter File "J:\Scraping\scrape-env\PythonWebScraping\py\04\core\file_blob_writer.py", line 3, in from interface import implements ModuleNotFoundError: No module named 'interface'

then i change the the import line from from interface import implements to

from zope.interface import implements
from core.i_blob_writer import IBlobWriter

class FileBlobWriter(implements(IBlobWriter)):
    def __init__(self, location="."):
        self._location = location

    def write(self, filename, contents):
        full_filename = self._location + "/" + filename
        print("Attempting to write {0} bytes to {1}:".format(len(contents), filename))

        with open(full_filename, 'wb') as outfile:
            outfile.write(contents)

        print("The write was successful")

I got new error

Traceback (most recent call last): File "J:/Scraping/scrape-env/PythonWebScraping/py/04/07_create_image_thumbnail.py", line 3, in from core.file_blob_writer import FileBlobWriter File "J:\Scraping\scrape-env\PythonWebScraping\py\04\core\file_blob_writer.py", line 6, in class FileBlobWriter(implements(IBlobWriter)): File "C:\Users\Kan\Anaconda3\lib\site-packages\zope\interface\declarations.py", line 483, in implements raise TypeError(_ADVICE_ERROR % 'implementer') TypeError: Class advice impossible in Python3. Use the @implementer class decorator instead.

And my new code is:

from zope.interface import implements
from core.i_blob_writer import IBlobWriter

@implementer(IBlobWriter)
class FileBlobWriter:
    def __init__(self, location="."):
        self._location = location

    def write(self, filename, contents):
        full_filename = self._location + "/" + filename
        print("Attempting to write {0} bytes to {1}:".format(len(contents), filename))

        with open(full_filename, 'wb') as outfile:
            outfile.write(contents)

        print("The write was successful")

It doesn't work neither

Traceback (most recent call last): File "J:/Scraping/scrape-env/PythonWebScraping/py/04/07_create_image_thumbnail.py", line 3, in from core.file_blob_writer import FileBlobWriter File "J:\Scraping\scrape-env\PythonWebScraping\py\04\core\file_blob_writer.py", line 6, in @implementer(IBlobWriter) NameError: name 'implementer' is not defined

I completely stuck now Could the author explain the problem, thanks