Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:58:42+00:00 2026-06-12T03:58:42+00:00

How can I parse with python beautifulsoup the following code? I need to get

  • 0

How can I parse with python beautifulsoup the following code? I need to get each image with its corresponding width and height properties (if they exist).

The code below “means there are 3 images on this page, the first image is 300×300, the middle one has unspecified dimensions, and the last one is 1000px tall” (as explained here)

<meta property="og:image" content="http://example.com/rock.jpg" />
<meta property="og:image:width" content="300" />
<meta property="og:image:height" content="300" />
<meta property="og:image" content="http://example.com/rock2.jpg" />
<meta property="og:image" content="http://example.com/rock3.jpg" />
<meta property="og:image:height" content="1000" />

So far I have the following code, but it only returns the first set of dimensions:

images = []
img_list = soup.findAll('meta', {"property":'og:image'})
for og_image in img_list:
    if not og_image.get('content'):
        continue

    image = {'url': og_image['content']}

    width = self.soup.find('meta', {"property":'og:image:width'})
    if width:
        image['width'] = width['content']
    height = self.soup.find('meta', {"property":'og:image:height'})
    if width:
        image['height'] = height['content']

    images.append(image)

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:58:43+00:00Added an answer on June 12, 2026 at 3:58 am

    I want something fast, which uses beautifulsoup tree structure. Here is the solution I found suitable, in case there are people looking for something similar:

    from BeautifulSoup import BeautifulSoup, Tag
    
    soup = BeautifulSoup(html)
    images = []
    image = {}
    
    img_list = soup.findAll('meta', {"property":'og:image'})
    for og_image in img_list:
        if not og_image.get('content'):
            continue
    
        image = {'url': og_image['content']}
        next = og_image.nextSibling.nextSibling # calling once returns end of line char '\n'
    
        if next and isinstance(next, Tag) and next.get('property', '').startswith('og:image:'):
            dimension = next['content']
            prop = next.get('property').rsplit(':')[-1]
            image[prop] = dimension
    
            next = next.nextSibling.nextSibling
            if next and isinstance(next, Tag) and next.get('property', '').startswith('og:image:'):
                dimension = next['content']
                prop = next.get('property').rsplit(':')[-1]
                image[prop] = dimension
    
        images.append(image)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a class, preferably written in PHP, C#, or Python, that can parse
I need google page rank get in python. There i can officially extract it?
Is there a parser that: is able to parse Python 2.6+ code, and can
I am using python + BeautifulSoup to parse an HTML document. Now I need
Is there a Python library that can parse an CSS selector and emit an
How can I parse a string like 01-Jan-1995 to a Python datetime object?
I need a PHP Regex that can parse .strings files. In particular, it needs
I need a function that can parse both 1,123.12 and 1.123,12 to 1123.12 There
Can anyone direct me to a good Python screen scraping library for javascript code
How can I parse and read an outlook pst file in Python?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.