I want to scrape price and status of website. I am able to scrape price but unable to scrape status. Couldn’t find in JSON as well.
here is link: https://www.zoro.com/jonard-tools-diagonal-cutting-plier-8-l-jic-2488/i/G2736212/?recommended=true
from requests import get from bs4 import BeautifulSoup resp = get(url) soup = BeautifulSoup(resp.text, 'lxml') # print(soup.prettify()) price = soup.find('div', class_ = 'product-price') status = soup.find('div', class_ = 'avl-status buy-box__shipping-item') print(status.text)
Advertisement
Answer
You can use Json microformat embedded inside the page to obtain availability (price, images, description…).
For example:
import json import requests from bs4 import BeautifulSoup url = "https://www.zoro.com/jonard-tools-diagonal-cutting-plier-8-l-jic-2488/i/G2736212/?recommended=true" soup = BeautifulSoup( requests.get(url).content, 'html.parser' ) data = json.loads(soup.select_one('script[type="application/ld+json"]').contents[0]) # uncomment this to print all data: # print(json.dumps(data, indent=4)) print('Price : ', data['offers']['price']) print('Availability: ', data['offers']['availability'])
Prints:
Price : 17.13 Availability: http://schema.org/InStock
EDIT: You can observe all product data that is embedded within the page:
import json import requests from bs4 import BeautifulSoup url = "https://www.zoro.com/baldwin-filters-filter-service-kit-thermo-king-bk6092/i/G1609513/" # url = 'https://www.zoro.com/jonard-tools-diagonal-cutting-plier-8-l-jic-2488/i/G2736212/?recommended=true' soup = BeautifulSoup( requests.get(url).content, 'html.parser' ) data = json.loads(soup.select_one('div.hidden[data-state]')['data-state'] ) # uncomment this to print all data: # print(json.dumps(data, indent=4)) _, product_data = data['product']['productDetailsData'].popitem() print(json.dumps(product_data, indent=4)) print() print('isExpeditable = ', product_data['isExpeditable'])
When this key isExpeditable
is set to False
, it means Drop Shipping (I think). When I tested it with product that is in stock, it prints True
.
The output:
{ "packageQty": 1, "isMotorCompliant": false, "zoroNo": "G1609513", "brand": "Baldwin Filters", "salesStatus": "TP", "orderChannel": "Default", "description": "Filter Service Kit, For Vehicle Type - Filter Kits Thermo King, Includes Lube Spin-On, Fuel, Water Separator Element, Fuel Spin-On", "restrictedStates": [], "title": "Filter Service Kit", "categoryPaths": [ [ { "name": "Automotive Filters", "slug": "automotive-filters", "code": "7540" }, { "name": "Filter Service Kits", "slug": "filter-service-kits", "code": "10660" } ] ], "restrictedSaleItemCode": "", "slug": "baldwin-filters-filter-service-kit-thermo-king-bk6092", "energyGuideLabelFileName": "", "variants": null, "isForcedOutOfStock": false, "lightingFactLabelFileName": "", "isExpeditable": false, "erpId": "2770121", "californiaProp65Message": null, "isHazmat": false, "leadTime": 8, "mfrNo": "BK6092", "attributes": [ { "name": "For Vehicle Type - Filter Kits", "value": "Thermo King" }, { "name": "Item", "value": "Filter Service Kit" }, { "name": "For Use With", "value": "Thermo King" }, { "name": "Includes", "value": "Lube Spin-On, Fuel, Water Separator Element, Fuel Spin-On" }, { "name": "Country of Origin (subject to change)", "value": "United States" } ], "originalPrice": null, "isCircleECompliant": false, "lowLeadComplianceLevel": "", "priceUnit": "EA", "isDropShipDirect": false, "minRetailQty": 1, "price": 118.29, "media": [ { "name": "Z1qr7ymcpEx_.JPG", "type": "image/jpeg" } ] } isExpeditable = False