I can't extract image src's. The class in the code is the class of the img tag. I got a KeyError when I tried to use another tag's class. How can i get the src of the image?
from base64 import decode
import requests
from bs4 import BeautifulSoup
import pandas as pd
import csv
from time import sleep
from random import randint
import numpy as np
headers = dict()
headers[
"User-Agent"
] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
images = []
titles = []
authors = []
pages = np.arange(1, 2, 1)
for page in pages:
url = "https://www.dr.com.tr/kategori/Kitap/Cocuk-ve-Genclik/grupno=00884?Page=" + str(page)
results = requests.get(url, headers=headers)
soup = BeautifulSoup(results.text, "html.parser")
book_div = soup.find_all("div", class_="prd-main-wrapper")
sleep(randint(2, 10))
for bookSection in book_div:
all_imgs = [img["src"] for img in soup.select(".lazyloaded")]
images.append(all_imgs)
print(all_imgs)
name = bookSection.find("a", class_="prd-name").get('title')
titles.append(name)
author = bookSection.find("div", class_="prd-row").text.strip()
authors.append(author)
books = pd.DataFrame(
{
"Image": images,
"Book": titles,
"Author": authors,
}
)
books.to_csv("dr_child.csv", index=False, header=True,encoding = 'utf-8-sig')
As a result, only this [] returns.