How can I use the json module to extract the price from provides the data in JSON format in an inline script?
I tried to extract the price in https://glomark.lk/top-crust-bread/p/13676 But I couldn't to get the price value.
So please help me to solve this.
import requests
import json
import sys
sys.path.insert(0,'bs4.zip')
from bs4 import BeautifulSoup
user_agent = {
'User-agent': 'Mozilla/5.0 Chrome/35.0.1916.47'
}
headers = user_agent
url = 'https://glomark.lk/top-crust-bread/p/13676'
req = requests.get(url, headers = headers)
soup = BeautifulSoup(req.content, 'html.parser')
products = soup.find_all("div", class_ = "details col-12 col-sm-12
col-md-6 col-lg-5 col-xl-5")
for product in products:
product_name = product.h1.text
product_price = product.find(id = 'product-promotion-price').text
print(product_name)
print(product_price)
JavaScriptto add/update elements butBeautifulSoup/lxml,requests/urllibcan't runJS. You may need Selenium to control real web browser which can runJS. OR use (manually)DevToolsinFirefox/Chrome(tabNetwork) to see ifJavaScriptreads data from some URL. And try to use this URL withrequests.JSusually getsJSONwhich can be easy converted to Python dictionary (withoutBS). You can also check if page has (free)APIfor programmers.