Home>
Insert image in data frame

The information scraped from the specified URL is put in the data frame by Pandas.
So I want to insert scraped information and images in the same table (separate column)

I feel like the following.

Images | Information
---------------
0 | Displayed image | Character
---------------
1 | Image displayed | Character
-----------------

List URLs

Scraped items are now in the list.
The URL is also listed, but it is unknown how to insert it as an image from there.

Code
from selenium import webdriver
import pandas as pd
browser = webdriver.Chrome ()
browser.get ('url')
#image
logos = browser.find_elements_by_class_name ('class_name')
image_info = []
for logo in logos:
    logo = logo.find_element_by_tag_name ('img')
    img = logo.get_attribute ('src')
    image_info.append (img)
df = pd.DataFrame ()
df ['image'] = image_info
df
Problem

If this is the case, the URL link of the image will be in the column,

Images will not be displayed and will be entered as text.

Is it possible to insert it into a data frame as an image?

  • Answer # 1

      

    If this is the case, the URL link of the image will be entered in the column.
      The image will not be displayed and will be entered as text.

    You need to download the image data separately. It's relatively easy to userequests.

      

    Is it possible to insert it into a data frame as an image?

    I don't understand the requirement of "as an image", but binary data can be inserted as it is.

    Sample code that performs the above operations is shown below.

    from selenium import webdriver
    driver = webdriver.Chrome (executable_path = r "~")
    url = 'https://www.python.org/'
    driver.get (url)
    from urllib.parse import urljoin
    import requests
    srcs, imgs = [], [] # Source URL and image data
    elems = driver.find_elements_by_tag_name ('img')
    for e in elems:
        src = e.get_attribute ('src')
        img = requests.get (urljoin (url, src)). content # Download image data
        srcs.append (src)
        imgs.append (img)
    driver.quit ()
    # Build data frame and save to CSV
    import pandas as pd
    df = pd.DataFrame ({'src': srcs, 'img': imgs})
    df.to_csv ('ret.csv', index = False)

    ret.csv

    src, img
    https://www.python.org/static/img/python-logo.png, "b '\ x89PNG \ r \ n \ x1a ~ omitted ~

    However, if you want to put an image into a data frame, it is better to download it to a separate image directory and give the data frame only the path information to the image file.
    Please refer to the following page for the reason.
    Is it possible to store images in a database?