Django,通过ajax发送beautifulsoup解析的html给出的不是JSON序列化错误

时间:2015-12-25 22:29:40

标签: django beautifulsoup

我使用beautifulsoup解析html并尝试通过JsonResponse将img元素传递给模板。

我的观点:

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 in range(len(images)):
        imagesDict[i] = images[i]

    return JsonResponse(imagesDict)

但是我收到了这个错误:

TypeError at /spider/ <img alt="" height="23" onclick="(function(){var text_input_assistant_js='/textinputassistant/11/tr_tia.js';var s = document.createElement('script');s.src = text_input_assistant_js;(document.getElementById('xjsc')|| document.body).appendChild(s);})();" src="/textinputassistant/tia.png" style="padding-top:2px" width="27"/> is not JSON serializable

1 个答案:

答案 0 :(得分:2)

你不能这样做,因为BeautifulSoup findAll返回的是一个漂亮的汤对象列表,但json只能有普通类型。这个错误一般说&#34;你试图序列化的不是json识别格式&#34;。你可以这样做:

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)