[Answered ]-Django, sending beautifulsoup parsed html via ajax gives is not JSON serializable error

2👍

You cannot do that, because what BeautifulSoup findAll returns is a list of beautiful soup objects, but json can only have plain types. That error in general says “what you are trying to serialize is not in json recognizable format”. You can do:

def spider(request):
    r = requests.get("https://www.google.com.tr/search?q=cizre&biw=1438&bih=802&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiu8any3vLJAhWBkywKHfEbAeoQ_AUICCgD#imgrc=_")
    soup = BeautifulSoup(r.content, "html.parser")
    images = soup.findAll("img")
    imagesDict = {}
    for i, image in enumerate(images):
        # convert object to string
        imagesDict[i] = str(image)
    return JsonResponse(imageDict)

Leave a comment