POST Requests
Introduction
In the provided code, the objective is to gather stock details from a product page using both GET and POST requests. The initial part involves making a GET request to a specified URL, extracting relevant information from the HTML response, such as Stock Number and Case Pack size. Later, a POST request is demonstrated where the gathered data is used as parameters to obtain stock details. The final output includes information about Stock Cases and Stock Units.
Problem Statement
Get data points : Titles, Stock number, Case Packs and Get Stock Details : 'Out of stock' Or 'how much in stock'
Solution
First, Using get request, get data points from html required for post requests "parameter or form data" - parameter require "Stock_Number, Product_Id & Case".
Required data points are :
Product title (Title)
Stock Number.
Code Snippet
import requests
from lxml.html import fromstring
url = 'https://www.sks-bottle.com/product/4215.html'
response = requests.get(url)
# print(f'STATUS CODE : {response.status_code}')
# parser
parser = fromstring(response.content)
# title of page
Title = parser.findtext('.//title')
Title = str(Title.replace("\n", " ").replace("\t", " ")).strip()
# print("Title : ", Title)
# get - Product_Code will be required for api request.
Stock_Number = parser.xpath('.//div[@class="product_detail_specs"]//b[contains(text(), "Stock #:")]/following-sibling::span/text()')[0]
Stock_Number = str(Stock_Number.replace("\n", " ").replace("\t", " ")).strip()
# print(f'Stock_Number : {Stock_Number}')
Output :

Get Stock Detail - with post request

Post request header


Stock detail in post request url - Output required
Parameters required

POST - Requests
Code : Get Stock Detail (Post Requests)
# Get - Stock Detail - With "POST" requests.
# required post request = url
post_url = 'https://www.sks-bottle.com/includes/product_inventory.php'
# get - Product_Id : from product page url
Product_Id = url.replace(".html","").split("/")[-1]
# prepare form data or paramter required for post request
data = {
'stock_numbers': f'{Stock_Number}',
'case_packs': f'{Case_Packs}',
'case_packs_single': f'{Case_Packs}',
'product_types': '',
'product_ids': f'{Product_Id}',
'location': 'product'}
# post requests
post_response = requests.post(post_url, data=data)
print("POST STATUS CODE : ",post_response.status_code)
# response in json form
json_data = post_response.json()
print(f'Check json_data type : {type(json_data)}')
print(f'json_data :\n {json_data} \n')
Stock_Case = json_data['StockInfoCases']
Stock_Unit = json_data['StockInfoUnits']
print('STOCK DETAIL :')
print(f'{Stock_Case} : {Stock_Unit}')
Output :
