meyt / linkpreview

Get link preview in python
MIT License
46 stars 9 forks source link

TypeError: argument of type 'NoneType' is not iterable in v0.8.2 #25

Closed asmaier closed 10 months ago

asmaier commented 10 months ago

The following test code will throw an exception

from linkpreview import link_preview, Link, LinkPreview
import requests

urls = [
    "http://www.heise.de/open/meldung/Deutschlands-erstes-Portal-fuer-offene-Daten-in-Berlin-1558417.html"
]

headers ={'User-Agent': 'Facebot'}
session = requests.Session()

for url in urls:
    r = session.get(url, headers=headers, timeout=10)
    print("Status code:", r.status_code)
    link = Link(r.url, r.text)
    preview = LinkPreview(link)
    print("title:", preview.title)
    print("description:", preview.description)
    print("site_name:", preview.site_name)
    print("force_title:", preview.force_title)
    print("image:", preview.image)
    print("absolute_image:", preview.absolute_image)
    print("------")

The output is

Status code: 200
title: Deutschlands erstes Portal für offene Daten in Berlin
description: Berlin hat jetzt das erste deutsche Open-Data-Portal errichtet und spielt den Vorreiter für mehr Demokratie.
site_name: iX Magazin
force_title: Deutschlands erstes Portal für offene Daten in Berlin
Traceback (most recent call last):
  File "/Users/<user>/git/social2blog/linkpreview_tests.py", line 37, in <module>
    print("image:", preview.image)
  File "/Users/<user>/.local/share/virtualenvs/social2blog-daDco7w8/lib/python3.9/site-packages/linkpreview/helpers.py", line 33, in __get__
    val = f(obj)
  File "/Users/<user>/.local/share/virtualenvs/social2blog-daDco7w8/lib/python3.9/site-packages/linkpreview/linkpreview.py", line 50, in image
    return self._find_attribute("image")
  File "/Users/<user>/.local/share/virtualenvs/social2blog-daDco7w8/lib/python3.9/site-packages/linkpreview/linkpreview.py", line 32, in _find_attribute
    value = getattr(obj, name)
  File "/Users/<user>/.local/share/virtualenvs/social2blog-daDco7w8/lib/python3.9/site-packages/linkpreview/preview/schemabase.py", line 67, in image
    return self._extract_image(item["image"])
  File "/Users/<user>/.local/share/virtualenvs/social2blog-daDco7w8/lib/python3.9/site-packages/linkpreview/preview/schemabase.py", line 56, in _extract_image
    if 'url' not in v:
TypeError: argument of type 'NoneType' is not iterable    

I believe the problem was newly created in v0.8.2, because that link used to work with earlier versions of linkpreview.

asmaier commented 9 months ago

Thank you. I can confirm in v0.8.3 the problem is fixed.