D
D
dooble2020-02-03 15:25:15
Python
dooble, 2020-02-03 15:25:15

How to parse python bs4 data?

<html><body><p>[{"productId":11804034,"productCode":"1438196","productPrice":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125810000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125810000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125810000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125810000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false},"variants":[{"id":11804263,"variantId":11804263,"sku":"8312581","isInStock":true,"isLowInStock":false,"price":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125810000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125810000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125810000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125810000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false}},{"id":11804041,"variantId":11804041,"sku":"8312583","isInStock":true,"isLowInStock":false,"price":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125830000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125830000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125830000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125830000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false}},{"id":11804269,"variantId":11804269,"sku":"8312585","isInStock":true,"isLowInStock":true,"price":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125850000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125850000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125850000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125850000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false}},{"id":11804293,"variantId":11804293,"sku":"8312579","isInStock":true,"isLowInStock":false,"price":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125790000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125790000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125790000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125790000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false}},{"id":11804376,"variantId":11804376,"sku":"8312586","isInStock":true,"isLowInStock":true,"price":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125860000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125860000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125860000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125860000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false}},{"id":11804251,"variantId":11804251,"sku":"8312587","isInStock":true,"isLowInStock":true,"price":{"current":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125870000166999","conversionId":null},"previous":{"value":3290.0,"text":"3 290,00 руб.","versionId":"REGP000002000800083125870000166999","conversionId":null},"rrp":{"value":null,"text":null,"versionId":"REGP000002000800083125870000166999","conversionId":null},"xrp":{"value":38.69,"text":"38,69 руб.","versionId":"REGP000002000800083125870000166999","conversionId":"0"},"currency":"RUB","isMarkedDown":false,"isOutletPrice":false}}]}]</p></body></html>


The problem is that I can't figure out how to get the price value here, i.e. RUB 3,290.00 if you make it through request.find().text
then just go away</p></body></html>.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-02-03
@dooble

import json
from lxml import etree

str1 = '<ваша строка>'
root = etree.fromstring(str1)

data = json.loads(root.xpath('.//p')[0].text)
print(data[0]['productPrice']['current']['value'])

> 3290.0
instead of lxml . similarly xpath can be found in bs4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question