Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to access object properties in json using beautifulsoup? python

from bs4 import BeautifulSoup
import fake_useragent
import requests
ua = fake_useragent.UserAgent()
import soupsieve as sv


url = "https://search-maps.yandex.ru/v1/?text=%D0%9F%D0%BE%D1%87%D1%82%D0%B0%20%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D0%B8,%20%D0%9A%D1%80%D0%B0%D1%81%D0%BD%D0%BE%D0%B4%D0%B0%D1%80&results=500&type=biz&lang=ru_RU&apikey=d9168899-cf24-452a-95cf-06d7ac5a982b"
r = requests.get(url, headers={"User-Agent": ua.random})
soup = BeautifulSoup(r.text, 'lxml')
print(soup.find("p"))

i want to choose from this list only two properties like "boundedBy" and "coordinates"
How can i do it?I ve checked the whole bs documentation, but didnt find a solution

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use the .json() method of the response, since the data is JSON. You can then iterate over the features in the response. Note you can set the parameters separate from the URL so they are readable and easier to change:

import requests
import json

url = 'https://search-maps.yandex.ru/v1'

params = {'text': 'Почта России, Краснодар',
          'results': 500,
          'type': 'biz',
          'lang': 'ru_RU',
          'apikey': 'd9168899-cf24-452a-95cf-06d7ac5a982b'}

r = requests.get(url, params=params)
if r.ok:
    data = r.json()
    for feature in data['features']:
        x,y = feature["geometry"]["coordinates"]
        (x1,y1),(x2,y2) = feature["properties"]["boundedBy"]
        print(f'coordinates ({x:.6f}, {y:.6f}): bounds ({x1:.6f}, {y1:.6f})-({x2:.6f}, {y2:.6f})')

Output:

coordinates (38.969711, 45.028356): bounds (38.965656, 45.025508)-(38.973867, 45.031330)
coordinates (38.969660, 45.028365): bounds (38.965656, 45.025508)-(38.973867, 45.031330)
coordinates (38.993199, 45.063675): bounds (38.989012, 45.060879)-(38.997223, 45.066698)
coordinates (39.029821, 45.048676): bounds (39.025753, 45.045304)-(39.033964, 45.051124)
coordinates (38.992736, 45.034352): bounds (38.988590, 45.031496)-(38.996801, 45.037318)
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading