Extract Image URLs by Class Name using PyQuery

  • Share this:

Code introduction


This function uses the PyQuery library to parse HTML content and find all images with a specified class name, then returns a list of the URLs of these images.


Technology Stack : PyQuery

Code Type : HTML parsing and extraction

Code Difficulty : Intermediate


                
                    
def find_all_images_with_class(html_content, class_name):
    from pyquery import PyQuery as pq
    
    # Use PyQuery to parse the HTML content
    doc = pq(html_content)
    
    # Find all images with the specified class name
    images = doc('img.' + class_name)
    
    # Return a list of image URLs
    return [img.attr('src') for img in images]                
              
Tags: