'Get gtin from <script type="application/ld+json"> using regex and/or xpath | source code

I need to extract the gtin value (8806090325632) I need to get the xpath expression to scrape the 13 digit gtin code only.

Here is the <script type="application/ld+json"> code :

<script type="application/ld+json"> {"@type":"Product","aggregateRating":{"@type":"AggregateRating","ratingValue":4.4946996466431095,"ratingCount":566,"bestRating":5.0,"worstRating":1.0},"brand":{"@type":"Organization","name":"SAMSUNG"},"description":"SAMSUNG 65TU7022 TV LED 4K - 65 \" (163cm) - HDR10 + - Dolby Digital Plus - Smart TV - 2xHDMI - 1xUSB - Classe énergétique G","gtin":"8806090325632","offers":{"@type":"Offer","availability":"InStock","itemCondition":"NewCondition","price":615.99,"priceCurrency":"EUR","priceValidUntil":"2022-04-06T14:57:01.7797852+02:00","url":"https://www.cdiscount.com/high-tech/televiseurs/samsung-65tu7022-tv-led-4k-uhd-65-163-cm-h/f-1062613-samue65tu7022.html?idOffre=639618765"},"productID":"SAMUE65TU7022","category":"Téléviseur LED","sku":"samue65tu7022","review":[{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"5","worstRating":"1"},"author":"Soso","datePublished":"2022-04-03T03:54:10","description":"Je recommande cet achat tout est parfait","name":"Parfait"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"4","worstRating":"1"},"author":"SmashingQuasar","datePublished":"2022-04-02T14:13:11","description":"Cette télévision est grande et offre une résolution 4K pour un prix abordable. La qualité d'image est correcte sans être exceptionnelle. Le HDR est anecdotique sur ce modèle, on ne le discerne presque pas. 4 étoiles pour ce modèle simplement car la profondeur de noir est insatisfaisante. En dehors de ça, une assez bonne affaire pour ceux qui souhaitent passer à la 4K.","name":"Une grande TV 4K peu chère"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"5","worstRating":"1"},"author":"luna","datePublished":"2022-04-01T07:40:28","description":"Sa taille et les options sont au top…","name":"TRES BON CHOIX"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"4","worstRating":"1"},"author":"Sylva","datePublished":"2022-03-28T13:27:19","description":"Je viens d'acheter ce téléviseur mais malheureusement pas de connection Bluetooth possible. S'il faille le contacter avec avec une barre de son Bluetooth n'y rêvez même pas. A part ça tout est parfait pour l'instant","name":"Pas de connexion Bluetooth possible"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"4","worstRating":"1"},"author":"Mortimer","datePublished":"2022-03-27T20:07:23Z","description":"L'image est superbe, quant à ses fonctionnalités, elles sont tellement nombreuses que je n'ai encore pas eu le temps de toutes les tester...","name":"Une belle bête !"}],"@context":"https://schema.org","name":"SAMSUNG 65TU7022 TV LED 4K UHD - 65 \" (163 cm) - HDR10 + - Dolby Digital Plus - Smart TV - 2xHDMI - 1xUSB","image":"https://www.cdiscount.com/pdt2/0/2/2/4/550x550/samue65tu7022/rw/samsung-65tu7022-tv-led-4k-uhd-65-163-cm-h.jpg","url":"https://www.cdiscount.com/high-tech/televiseurs/samsung-65tu7022-tv-led-4k-uhd-65-163-cm-h/f-1062613-samue65tu7022.html"}</script>



Solution 1:[1]

While there is no explicit language tag mentioned this answer point out approach with python.

Select the <script> from HTML and load extracted text with json.loads():

data = json.loads(soup.select_one('script[type="application/ld+json"]').text)

Now you could pick the gtin by key:

data['gtin']

Example

html = r'''<script type="application/ld+json">{"@type":"Product","aggregateRating":{"@type":"AggregateRating","ratingValue":4.4946996466431095,"ratingCount":566,"bestRating":5.0,"worstRating":1.0},"brand":{"@type":"Organization","name":"SAMSUNG"},"description":"SAMSUNG 65TU7022 TV LED 4K - 65 \" (163cm) - HDR10 + - Dolby Digital Plus - Smart TV - 2xHDMI - 1xUSB - Classe énergétique G","gtin":"8806090325632","offers":{"@type":"Offer","availability":"InStock","itemCondition":"NewCondition","price":615.99,"priceCurrency":"EUR","priceValidUntil":"2022-04-06T14:57:01.7797852+02:00","url":"https://www.cdiscount.com/high-tech/televiseurs/samsung-65tu7022-tv-led-4k-uhd-65-163-cm-h/f-1062613-samue65tu7022.html?idOffre=639618765"},"productID":"SAMUE65TU7022","category":"Téléviseur LED","sku":"samue65tu7022","review":[{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"5","worstRating":"1"},"author":"Soso","datePublished":"2022-04-03T03:54:10","description":"Je recommande cet achat tout est parfait","name":"Parfait"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"4","worstRating":"1"},"author":"SmashingQuasar","datePublished":"2022-04-02T14:13:11","description":"Cette télévision est grande et offre une résolution 4K pour un prix abordable. La qualité d'image est correcte sans être exceptionnelle. Le HDR est anecdotique sur ce modèle, on ne le discerne presque pas. 4 étoiles pour ce modèle simplement car la profondeur de noir est insatisfaisante. En dehors de ça, une assez bonne affaire pour ceux qui souhaitent passer à la 4K.","name":"Une grande TV 4K peu chère"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"5","worstRating":"1"},"author":"luna","datePublished":"2022-04-01T07:40:28","description":"Sa taille et les options sont au top…","name":"TRES BON CHOIX"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"4","worstRating":"1"},"author":"Sylva","datePublished":"2022-03-28T13:27:19","description":"Je viens d'acheter ce téléviseur mais malheureusement pas de connection Bluetooth possible. S'il faille le contacter avec avec une barre de son Bluetooth n'y rêvez même pas. A part ça tout est parfait pour l'instant","name":"Pas de connexion Bluetooth possible"},{"@type":"Review","reviewRating":{"@type":"Rating","bestRating":"5","ratingValue":"4","worstRating":"1"},"author":"Mortimer","datePublished":"2022-03-27T20:07:23Z","description":"L'image est superbe, quant à ses fonctionnalités, elles sont tellement nombreuses que je n'ai encore pas eu le temps de toutes les tester...","name":"Une belle bête !"}],"@context":"https://schema.org","name":"SAMSUNG 65TU7022 TV LED 4K UHD - 65 \" (163 cm) - HDR10 + - Dolby Digital Plus - Smart TV - 2xHDMI - 1xUSB","image":"https://www.cdiscount.com/pdt2/0/2/2/4/550x550/samue65tu7022/rw/samsung-65tu7022-tv-led-4k-uhd-65-163-cm-h.jpg","url":"https://www.cdiscount.com/high-tech/televiseurs/samsung-65tu7022-tv-led-4k-uhd-65-163-cm-h/f-1062613-samue65tu7022.html"}</script>'''
soup = BeautifulSoup(html)

data = json.loads(soup.select_one('script[type="application/ld+json"]').text)
data['gtin']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HedgeHog