0

Вот код страницы, которую надо спарсить:

<html>
    <head>
        <meta name="color-scheme" content="light dark">
    </head>
    <body>
        <pre style="word-wrap: break-word; white-space: pre-wrap;">
            {"contractData":{"amount":6414680,"owner_address":"TMXSPuwvw3A1PcMB2VssrXKfF26ANzfrcj","to_address":"T9yssSMcwoSXkh4jVpXG8KPPzeAyA5WQk6"}}
        </pre>
    </body>
</html>

В <p> есть amount и его значение, как их оттуда достать?

Вот мой код:

import requests
from bs4 import BeautifulSoup

transaction = 'тут ссылка на страницу'

response = requests.get(transaction).text
soup = BeautifulSoup(response, 'lxml')

2 ответа 2

2

Распарсить полученную строку как JSON и достать значение как из словаря. Псевдокод:

resAsStr = requests.get(transaction).text

resAsDict = json.loads(resAsStr)

amount = resAsDict["contractData"]["amount"]
1
  • спасибо за помощь
    – Code Lao
    13 янв в 16:07
2
import json
from bs4 import BeautifulSoup

txt ='''<html>
    <head>
        <meta name="color-scheme" content="light dark">
    </head>
    <body>
        <pre style="word-wrap: break-word; white-space: pre-wrap;">
            {"contractData":{"amount":6414680,"owner_address":"TMXSPuwvw3A1PcMB2VssrXKfF26ANzfrcj",
            "to_address":"T9yssSMcwoSXkh4jVpXG8KPPzeAyA5WQk6"}}
        </pre>
    </body>
</html>'''

soup = BeautifulSoup(txt, 'lxml')
print(json.loads(soup.pre.text)['contractData']['amount'])
1
  • спасибо, помогло!
    – Code Lao
    13 янв в 16:07

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.