Получит URL продукта и информацию о наличии товара на складе. Распечатайте эту информацию на консоль исохранить его в файл «stok-kontrol.csv»
Проверено: Python 3.7.4
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import re
chrome_driver_path = r'C:\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
# Gets whether the products in the array, are in stock, from www.amazon.com
# Returns an Array of Dictionaries, with keys ['asin','instock','url']
def IsProductsInStock(array_of_ASINs):
results = []
for asin in array_of_ASINs:
url = 'https://www.amazon.com/dp/'+str(asin)
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'lxml')
stock = soup.find(id='availability').get_text().strip()
isInStock = False
if('In Stock' in stock):
# If 'In Stock' is the text of 'availability' element
isInStock=True
else:
# If Not, extract the number from it, if any, and see if it's in stock.
tmp = re.search(re.compile('[0-9]+'), stock)
if( tmp is not None and int(tmp[0]) > 0):
isInStock = True
results.append({"asin": asin, "instock": isInStock, "url": url})
return results
# Saves the product information to 'toFile'
# Returns a pandas.core.frame.DataFrame object, with the product info ['url', 'instock'] as columns
# inStockDict MUST be either a Dictionary, or a 'list' of Dictionaries with, ['asin','instock','url'] keys
def SaveProductInStockInformation(inStockDict, toFile):
if(isinstance(inStockDict, dict)):
stok_kontrol = pd.DataFrame( { 'Url': [inStockDict['url']], 'Stok Durumu': [inStockDict['instock']] } )
elif(isinstance(inStockDict, list)):
stocksSimple = []
for stock in inStockDict:
stocksSimple.append([stock['url'], stock['instock']])
stok_kontrol = pd.DataFrame(stocksSimple, columns=['Url', 'Stok Durumu'])
else:
raise Exception("inStockDict parm, Must be Either a dictionary, or a 'list' of dictionaries with, ['asin','instock','url'] keys!")
stok_kontrol.to_csv(toFile, encoding='utf-8-sig')
return stok_kontrol
# Get ASINs From File
f = open(r'C:\chromedriver_win32\asin.txt','r')
urls = f.read().split()
f.close()
# Get a list of Dictionaries containing all the products information
stocks = IsProductsInStock(urls)
# Save and Print the ['url', 'instock'] information
print( SaveProductInStockInformation(stocks, 'stok-kontrol.csv') )
# Remove if you need to use the driver later on in the program
driver.close()
Результаты: (файл 'stok-kontrol.csv')
,Url,Stok Durumu
0,https://www.amazon.com/dp/B00004SU18,True
1,https://www.amazon.com/dp/B07L9178GQ,True
2,https://www.amazon.com/dp/B01M35N6CZ,True