AttributeError: 'dict' object has no attribute 'split' in _get_absolute_image
asmaier opened this issue · 2 comments
asmaier commented
The following test code will throw an exception when trying to get the absolute_image
url
from linkpreview import link_preview, Link, LinkPreview
import requests
urls = [
"http://www.golem.de/0806/60182.html",
]
for url in urls:
preview = link_preview(url)
print("title:", preview.title)
print("description:", preview.description)
print("image:", preview.image)
print("force_title:", preview.force_title)
print("absolute_image:", preview.absolute_image)
print("site_name:", preview.site_name)
print("------")
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.4 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.4 facebookexternalhit/1.1 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("image:", preview.image)
print("force_title:", preview.force_title)
print("absolute_image:", preview.absolute_image)
print("site_name:", preview.site_name)
print("------")
The output is
title: Golem.de: IT-News für Profis
description: None
image: None
force_title: Golem.de: IT-News für Profis
absolute_image: None
site_name: www.golem.de
------
Status code: 200
title: RepRap - 3D-Drucker baut sich selbst - Golem.de
description: Der Brite Adrian Bowyer hat einen 3D-Drucker entwickelt, der auch für Heimnutzer erschwinglich ist. Mit diesem Gerät können die Nutzer die unterschiedlichsten Gegenstände aus
image: {'@type': 'ImageObject', 'url': '', 'width': 416, 'height': 234}
force_title: RepRap - 3D-Drucker baut sich selbst - Golem.de
Traceback (most recent call last):
File "/Users/<user>/git/social2blog/linkpreview_tests.py", line 35, in <module>
print("absolute_image:", preview.absolute_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 79, in absolute_image
return self._get_absolute_image(self.image)
File "/Users/<user>.local/share/virtualenvs/social2blog-daDco7w8/lib/python3.9/site-packages/linkpreview/linkpreview.py", line 54, in _get_absolute_image
parts = image.split("://")
AttributeError: 'dict' object has no attribute 'split'
In the second part of the example code we use a custom header. This seems to generate a different result for this website than without a header. But what is really strange is that preview.image
becomes a dict
. And it seems the code for generating the absolute_image
doesn't like that and throws an exception.
asmaier commented
Thank you very much for fixing the issue so quickly. I tried out the new version v0.8.2 and it runs fine now.