@dooble

Как спарсить данные Python bs4?

<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>


Проблема в том, что я не могу понять как тут достать значение цены, т.е. 3 290,00 руб. если делать это через request.find().text
То просто уходят </p></body></html>.
  • Вопрос задан
  • 232 просмотра
Решения вопроса 1
fox_12
@fox_12 Куратор тега Python
Расставляю биты, управляю заряженными частицами
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

вместо lxml . аналогичным образом xpath можно найти в bs4
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы