2020 Part 1, Chapter 2, Downloading Images from Bing

I am posting this so it might help people having problems with “Deep Learning for Coders with fastai and PyTorch,” Chapter 2. Specifically, when using Bing to download images (in the case of the book, some bear images), there was no output for the ‘ims’ variable using the first ‘search_images_bing’ command.

The first problem to solve is to get a valid Bing key. There are several other threads that tell how to do this. I found the following links useful (YMMV):

I was able to run the first ‘search_images_bing’ cell, but ‘ims’ was empty. The solution was to change ‘ims = results.attrgot(‘content_url’)’ to ‘ims = results.attrgot(‘contentUrl’)’. It appears the interface has changed from “snake-case” to “camel-case”. Please see a entry in the ‘results’ variable below.


{‘webSearchUrl’: ‘https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=teddy+bear&id=4F0E26E252CB0E110486422838F8FA743803ED1C&simid=608054600839201200’, ‘name’: “The History Of The Teddy Bear | The Sit 'n Sleep Blog”, ‘thumbnailUrl’: ‘https://tse3.mm.bing.net/th?id=OIP.o4OMl6yom_BbsBZyu38vgHaGC&pid=Api’, ‘datePublished’: ‘2020-04-18T19:53:00.0000000Z’, ‘isFamilyFriendly’: True, ‘contentUrl’: ‘https://www.sitnsleep.com/blog/wp-content/uploads/2019/04/teddy-bear.jpg’, ‘hostPageUrl’: ‘https://www.sitnsleep.com/blog/the-history-of-the-teddy-bear/’, ‘contentSize’: ‘4075637 B’, ‘encodingFormat’: ‘jpeg’, ‘hostPageDisplayUrl’: ‘https://www.sitnsleep.com/blog/the-history-of-the-teddy-bear’, ‘width’: 3180, ‘height’: 2592, ‘hostPageFavIconUrl’: ‘https://www.bing.com/th?id=ODF.ljVnFDazZi5-SwOMxCCvTA&pid=Api’, ‘hostPageDomainFriendlyName’: “Sit 'n Sleep”, ‘thumbnail’: {‘width’: 474, ‘height’: 386}, ‘imageInsightsToken’: 'ccid/o4OMl6ycp_06E75F72442ADDFBF70C4F52E6F871E0mid_4F0E26E252CB0E110486422838F8FA743803ED1Csimid_608054600839201200thid_OIP.!_o4OMl6yom!_BbsBZyu38vgHaGC’, ‘insightsMetadata’: {‘pagesIncludingCount’: 11, ‘availableSizesCount’: 8}, ‘imageId’: ‘4F0E26E252CB0E110486422838F8FA743803ED1C’, ‘accentColor’: ‘AE5307’}


Hi @JohnTD,
Good initiative, the thread starts to be long !
There is no need for key using this library :

Hi, I am getting the following error when trying to use your solution:

File “”, line 1
for q in [“grizzly”, “black bear”, “teddy bear”]:
SyntaxError: invalid character in identifier

Any idea???

Also, if there is no need for a key, how do you then handle the following (see the bolded line) a few cells later?

if not path.exists():
for o in bear_types:
dest = (path/o)
results = search_images_bing(key, f’{o} bear’)
download_images(dest, urls=results.attrgot(‘content_url’))

Update: For the syntax error above, I figured it out. It’s because I made a copy-paste and ended up with the wrong (exotic) quotation marks (instead of “…”, and ‘…’). For anyone having the same issue, just make sure after the copy-paste that you replace the quotation marks that appear with similar ones that you actually type in from your keyboard (not from the pasting).

For the second issue, I just removed that piece of code (the one that checks if the path exists) altogether.
But if you have a solution to keep it on, I would like to see it!