From 4c80c7117d85151544c55df13bda9cd7127f23f8 Mon Sep 17 00:00:00 2001 From: Priyankesh Date: Thu, 16 May 2024 23:11:58 +0530 Subject: [PATCH] #979 Made the required changes --- src/scrape_up/pinterest/pinterest.py | 16 ++++++++++++---- src/test/pinterest_test.py | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/scrape_up/pinterest/pinterest.py b/src/scrape_up/pinterest/pinterest.py index a5f2df99..20c8ea4f 100644 --- a/src/scrape_up/pinterest/pinterest.py +++ b/src/scrape_up/pinterest/pinterest.py @@ -128,11 +128,19 @@ def get_pin_details(self, pin_url): try: page = requests.get(pin_url) soup = bs(page.content, "html.parser") - title = soup.find("meta", property="og:title").get("content") - description = soup.find("meta", property="og:description").get("content") - saves = soup.find("meta", property="pinterestapp:saves").get("content") - comments = soup.find("meta", property="pinterestapp:comments").get("content") + + title_meta = soup.find("meta", property="og:title") + description_meta = soup.find("meta", property="og:description") + saves_meta = soup.find("meta", property="pinterestapp:saves") + comments_meta = soup.find("meta", property="pinterestapp:comments") + + title = title_meta.get("content") if title_meta else None + description = description_meta.get("content") if description_meta else None + saves = saves_meta.get("content") if saves_meta else None + comments = comments_meta.get("content") if comments_meta else None + return {"title": title, "description": description, "saves": saves, "comments": comments} except Exception as e: print("Error:", e) return None + diff --git a/src/test/pinterest_test.py b/src/test/pinterest_test.py index d2a3ec92..0aedb4a3 100644 --- a/src/test/pinterest_test.py +++ b/src/test/pinterest_test.py @@ -17,7 +17,7 @@ def test_get_today(self): self.assertIn('image', topic) def test_get_photo(self): - url = "https://www.pinterest.com/pin/1234567890/" # replace with a valid Pinterest pin URL + url = "https://pin.it/1ZhgQA5AG" photo = self.pinterest.get_photo(url) if photo: self.assertIn('alt', photo) @@ -33,7 +33,7 @@ def test_search_pins(self): self.assertIn('image', pin) def test_get_pin_details(self): - pin_url = "https://www.pinterest.com/pin/1234567890/" # replace with a valid Pinterest pin URL + pin_url = "https://pin.it/1ZhgQA5AG" details = self.pinterest.get_pin_details(pin_url) if details: self.assertIn('title', details)