【发布时间】:2021-12-06 02:23:21
【问题描述】:
我正在尝试抓取网站上有关产品的所有信息。我已经处理了所有的标题、描述、skus、价格并将它们放入 csv 中,但是我需要有一个列 images 来存储我下载图像的名称(最后的任务是导入它们全部使用magento2)。
问题在于,我认为并非所有图像都已下载,并且如果您要运行代码,则需要事先创建文件夹 images_test。
p>我将在此处附上我的代码。我真的需要在一天结束之前完成这个。
import requests
from bs4 import BeautifulSoup
import csv
import pandas as pd
import os
baseurl = 'https://www.k-beauty.ro/magazin/'
# an array for all the product links
productLinks = []
# going through all the pages of the shop
for x in range(1,8):
r = requests.get(f'https://www.k-beauty.ro/magazin/page/{x}')
soup = BeautifulSoup(r.content, 'lxml')
productlist = soup.find_all('div', class_= 'product-element-top')
# taking all the links to each product page
for item in productlist:
for link in item.find_all('a', href=True, class_='product-image-link'):
productLinks.append(link['href'])
# appending the links previously taken to the array
ProductItemsList = []
i = 0
j = 0
d={}
imagesList = []
folder = 'images_test'
rootFolder = os.getcwd()
os.chdir(folder)
# going through all the product pages and getting the info we need
for linkTest in productLinks:
r = requests.get(linkTest)
soup = BeautifulSoup(r.content, 'lxml')
title = soup.find('h1', class_='product_title').text.strip()
price = soup.find('p', class_ = 'price').text.strip()
header = soup.find('div', class_ = 'woocommerce-product-details__short-description').text.strip()
sku = soup.find('span', class_ = 'sku').text.strip()
categories = soup.find('span' , class_ = 'posted_in').text.strip()
description = soup.find('div', class_ = 'wc-tab-inner').text.strip()
brand = soup.find('div', id = 'tab-pwb_tab-content').text.strip()
# getting all the images on the website
images = soup.find_all('img', class_ = 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail')
for image in images:
if image not in d:
d[image]=1
else:
continue
i = i + 1
name = str(i) +'img'
i = int(i)
print(name)
link = image['src']
print(link)
# here I am adding the .jpg and saving the images
with open(name + '.jpg', 'wb') as f:
im = requests.get(link)
print("URMEAZA DEBUG: {}".format(im))
f.write(im.content)
imagesList.append(name)
# storing all the info about this product
for name in imagesList:
product = {
'sku': sku,
'categories': categories,
'name' : title,
'description': description,
'short_description': header,
'price' : price[0:5],
'images': name,
}
ProductItemsList.append(product)
os.chdir(rootFolder)
df =pd.DataFrame(ProductItemsList)
print(df)
df.to_csv('./K_Beauty_test.csv')
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: python pandas csv web-scraping beautifulsoup