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
JavaScript
x
46
46
1
import csv
2
import requests
3
from bs4 import BeautifulSoup
4
site_link = requests.get("websitenamehere").text
5
soup = BeautifulSoup(site_link,"lxml")
6
7
read_file = open("blogger.csv","w",encoding="UTF-8")
8
csv_writer = csv.writer(read_file)
9
csv_writer.writerow(["Header","links","Publish Time"])
10
counter = 0
11
12
for article in soup.find_all("article"):
13
###Counting lines
14
counter += 1
15
print(counter)
16
17
#Article Headers
18
headers = article.find("a")["title"]
19
print(headers)
20
21
#### Links
22
links = article.find("a")["href"]
23
print(links)
24
25
#### Publish time
26
publish_time = article.find("div",class_="mkdf-post-info-date entry-date published updated")
27
publish_time = publish_time.a.text.strip()
28
print(publish_time)
29
30
###image links
31
images = article.find("img",class_="attachment-full size-full wp-post-image nitro-lazy")["nitro-lazy-src"]
32
print(images)
33
34
###Download Article Pictures to disk
35
pic_name = f"{counter}.jpg"
36
with open(pic_name, 'wb') as handle:
37
response = requests.get(images, stream=True)
38
for block in response.iter_content(1024):
39
handle.write(block)
40
41
###CSV Rows
42
csv_writer.writerow([headers, links, publish_time])
43
print()
44
45
read_file.close()
46
Advertisement
Answer
You could basically convert to base64 and write to a file as you need it
JavaScript
1
6
1
import base64
2
3
with open("image.png", "rb") as image_file:
4
encoded_string= base64.b64encode(img_file.read())
5
print(encoded_string.decode('utf-8'))
6