Skip to content
Advertisement

is it possible to write image to csv file?

Hi everyone this is my first post here and wanted to know how can ı write image files that ı scraped from a website to a csv file or if its not possible to write on csv how can ı write this header,description,time info and image to a maybe word file Here is the code Everything works perfectly just wanna know how can ı write the images that i downloaded to disk to a csv or word file Thanks for your helps

import csv
import requests
from bs4 import BeautifulSoup
site_link = requests.get("websitenamehere").text
soup = BeautifulSoup(site_link,"lxml")

read_file = open("blogger.csv","w",encoding="UTF-8")
csv_writer = csv.writer(read_file)
csv_writer.writerow(["Header","links","Publish Time"])
counter = 0

for article in soup.find_all("article"):
    ###Counting lines
    counter += 1
    print(counter)

    #Article Headers
    headers = article.find("a")["title"]
    print(headers)

    #### Links
    links = article.find("a")["href"]
    print(links)

    #### Publish time
    publish_time = article.find("div",class_="mkdf-post-info-date entry-date published updated")
    publish_time = publish_time.a.text.strip()
    print(publish_time)

    ###image links
    images = article.find("img",class_="attachment-full size-full wp-post-image nitro-lazy")["nitro-lazy-src"]
    print(images)

    ###Download Article Pictures to disk
    pic_name = f"{counter}.jpg"
    with open(pic_name, 'wb') as handle:
        response = requests.get(images, stream=True)
        for block in response.iter_content(1024):
            handle.write(block)

    ###CSV Rows
    csv_writer.writerow([headers, links, publish_time])
    print()

read_file.close()

Advertisement

Answer

You could basically convert to base64 and write to a file as you need it

import base64

with open("image.png", "rb") as image_file:
    encoded_string= base64.b64encode(img_file.read())
    print(encoded_string.decode('utf-8'))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement