web analytics
Unlock the Magic of Your kitchen with Our Cookbook!

By: Mary

admin
By admin
5 Min Read
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

Are these sweet or tart cherries??? It’s almost tart cherry picking time in Door County (in Wisconsin).

version ai :
python
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse

def get_full_content_html(article_url: str) -> str:
“””
Fetches the HTML content from the given URL, extracts the main article content,
makes relative image and link URLs absolute, and returns the modified HTML.
Returns an empty string if the article content cannot be found or if the request fails.
“””
try:
response = requests.get(article_url, timeout=15)
response.raise_for_status() # Raise an exception for HTTP errors (like 404, 500)
except requests.exceptions.RequestException:
# If the request fails (e.g., network error, DNS, or HTTP error like 404 due to raise_for_status)
return “” # Return empty string as no article content could be retrieved.

soup = BeautifulSoup(response.text, ‘html.parser’)

# Construct the base URL for resolving relative paths.
# urljoin(article_url, ‘/’) ensures we get the scheme, netloc, and a trailing slash for proper base URL.
base_url = urljoin(article_url, ‘/’)

main_article_element = None

# Attempt to find the main article content.
# On Smitten Kitchen, recipe posts are typically within

tags with specific classes.
# We prioritize finding a ‘recipe-post’ article.
recipe_article = soup.find(‘article’, class_=’recipe-post’)
if recipe_article:
main_article_element = recipe_article
else:
# Fallback to finding any article with an ID starting with ‘post-‘, but explicitly check its title.
# This helps distinguish a true article from a 404 page that might also use

tags.
generic_article = soup.find(‘article’, id=lambda x: x and x.startswith(‘post-‘))
if generic_article:
title_element = generic_article.find(‘h1′, class_=’entry-title’)
# Only consider it a valid article if its title is not “Page Not Found”
if title_element and title_element.get_text(strip=True).lower() != ‘page not found’:
main_article_element = generic_article

article_content = None
if main_article_element:
# Once a valid article element is identified, find its specific content div.
article_content = main_article_element.find(‘div’, class_=’entry-content’)

if not article_content:
# If no valid article content is found (e.g., it’s a 404 page, or unexpected structure),
# return an empty string as per the implied request for “final article”.
return “”

# Process images within the article content
for img in article_content.find_all(‘img’):
# Prioritize ‘data-lazy-src’ for the ‘src’ attribute, common for lazy-loaded images
if img.has_attr(‘data-lazy-src’):
real_src = img[‘data-lazy-src’]
if not urlparse(real_src).netloc:
img[‘src’] = urljoin(base_url, real_src)
else:
img[‘src’] = real_src
del img[‘data-lazy-src’] # Remove the lazy-load attribute after copying
elif img.has_attr(‘src’):
img_src = img[‘src’]
# Convert relative ‘src’ URLs to absolute
if not urlparse(img_src).netloc:
img[‘src’] = urljoin(base_url, img_src)

# Prioritize ‘data-lazy-srcset’ for the ‘srcset’ attribute
srcset_str = None
if img.has_attr(‘data-lazy-srcset’):
srcset_str = img[‘data-lazy-srcset’]
del img[‘data-lazy-srcset’] # Remove the lazy-load attribute after copying
elif img.has_attr(‘srcset’):
srcset_str = img[‘srcset’]

if srcset_str:
srcset_values = srcset_str.split(‘,’)
new_srcset_values = []
for val in srcset_values:
parts = val.strip().split(‘ ‘)
if len(parts) > 0:
src_url = parts[0]
# Convert relative ‘srcset’ URLs to absolute
if not urlparse(src_url).netloc:
absolute_src_url = urljoin(base_url, src_url)
parts[0] = absolute_src_url
new_srcset_values.append(‘ ‘.join(parts))
img[‘srcset’] = ‘, ‘.join(new_srcset_values)

# Remove lazyload-related classes to ensure immediate display without JavaScript
if img.has_attr(‘class’):
img[‘class’] = [c for c in img[‘class’] if not c.startswith(‘lazy’)]

# Process links ( tags) within the article content
for a_tag in article_content.find_all(‘a’):
if a_tag.has_attr(‘href’):
href = a_tag[‘href’]
# Convert relative ‘href’ URLs to absolute, but ignore internal anchor links (#…)
if href and not urlparse(href).netloc and not href.startswith(‘#’):
absolute_href = urljoin(base_url, href)
a_tag[‘href’] = absolute_href

# Return the modified HTML content of the article
return str(article_content)

# The URL provided by the user
user_url = “https://smittenkitchen.com/2025/07/burrata-with-crushed-cherries-and-pistachios/#comment-2739128”

# Remove the comment anchor from the URL to get the base article URL
parsed_url = urlparse(user_url)
article_base_url = parsed_url.scheme + “://” + parsed_url.netloc + parsed_url.path

# Get the processed HTML content
final_article_html = get_full_content_html(article_base_url)

# Print the final article HTML without any additional explanation
print(final_article_html)

Share This Article
Leave a Comment