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

By: Peter Manifis

admin
By admin
3 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!

This web site inspires me!! The recipes are all tried and tested there is rarely a mistake!! Thank you for all your years on hard work and dedication! I am a trained chef cooking for 35years and you are still at the top of your game guys!!

version ai :

python
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

def get_full_article_html(url):

Remove the fragment identifier (e.g., #comment-2743284) from the URL

base_url = url.split('#')[0]

try:
    # User-Agent header to mimic a browser, sometimes helps with access
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(base_url, headers=headers)
    response.raise_for_status()  # Raise an HTTPError for bad responses (4xx or 5xx)
except requests.exceptions.RequestException:
    # If there's an error fetching the page, return an empty string
    # as per "return final article with out any explain information".
    return ""

soup = BeautifulSoup(response.text, 'html.parser')

# Find the main article content div
# On Smitten Kitchen, this is typically `div.entry-content`
article_content_div = soup.find('div', class_='entry-content')

if not article_content_div:
    # If the content div is not found, return an empty string.
    return ""

# Process images to ensure absolute URLs and handle lazy loading attributes
for img_tag in article_content_div.find_all('img'):
    # Handle lazy-loaded src
    if 'data-lazy-src' in img_tag.attrs:
        img_tag['src'] = urljoin(base_url, img_tag['data-lazy-src'])
        del img_tag['data-lazy-src']
    elif 'src' in img_tag.attrs: # Ensure src is absolute if not lazy-loaded
        img_tag['src'] = urljoin(base_url, img_tag['src'])
    else:
        # If no src or data-lazy-src, remove the img tag as it's likely broken or a placeholder.
        img_tag.decompose()
        continue # Move to the next image

    # Handle lazy-loaded srcset
    if 'data-lazy-srcset' in img_tag.attrs:
        srcset_values = img_tag['data-lazy-srcset'].split(',')
        processed_srcset_values = []
        for entry in srcset_values:
            parts = entry.strip().rsplit(' ', 1) # Split into URL and descriptor (e.g., '1024w')
            if len(parts) == 2:
                processed_srcset_values.append(f"{urljoin(base_url, parts[0])} {parts[1]}")
            else: # Only URL without descriptor
                processed_srcset_values.append(urljoin(base_url, parts[0]))
        img_tag['srcset'] = ', '.join(processed_srcset_values)
        del img_tag['data-lazy-srcset']
    elif 'srcset' in img_tag.attrs: # If there's a srcset but no data-lazy-srcset
        srcset_values = img_tag['srcset'].split(',')
        processed_srcset_values = []
        for entry in srcset_values:
            parts = entry.strip().rsplit(' ', 1)
            if len(parts) == 2:
                processed_srcset_values.append(f"{urljoin(base_url, parts[0])} {parts[1]}")
            else:
                processed_srcset_values.append(urljoin(base_url, parts[0]))
        img_tag['srcset'] = ', '.join(processed_srcset_values)

    # Remove other lazy-loading attributes
    if 'data-lazy-sizes' in img_tag.attrs:
        del img_tag['data-lazy-sizes']
    if 'loading' in img_tag.attrs and img_tag['loading'] == 'lazy':
        del img_tag['loading'] # No need for lazy loading if we want the image to load immediately

# Return the full HTML content of the article_content_div including its tags.
return str(article_content_div)

The URL provided points to a comment. The actual article URL is the base part.

article_url = “https://smittenkitchen.com/2025/04/simplest-brisket-with-braised-onions/#comment-2743284
final_article_html = get_full_article_html(article_url)
print(final_article_html)

Share This Article
Leave a Comment