I am using MongoEngine in a web clip project. I would like to keep track of all the images that I have encountered on all cleared web pages.
For this, I save the URL of the src image and the number of times the image was seen.
The definition of the MongoEngine model is as follows:
class ImagesUrl(Document): """ Model representing images encountered during web-scraping. When an image is encountered on a web-page during scraping, we store its url and the number of times it has been seen (default counter value is 1). If the image had been seen before, we do not insert a new document in collection, but merely increment the corresponding counter value. """
I am looking for a suitable way to implement the βsave or enlargeβ process.
So far I have been doing this this way, but I feel that there might be a better, inline way to do this with MongoEngine:
def save_or_increment(self): """ If it is the first time the image has been encountered, insert its src in mongo, along with a counter=1 value. If not, increment its counter value by 1. """
Is there a better way to do this?
Thanks so much for your time.
source share