Home > Back-end >  how to access object properties in json using beautifulsoup? python
how to access object properties in json using beautifulsoup? python

Time:11-17

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=Почта России, Краснодар&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

CodePudding user response:

The result from the server is in Json format, so use json parser or .json() method to decode it:

import json
import requests


url = "https://search-maps.yandex.ru/v1/?text=Почта России, Краснодар&results=500&type=biz&lang=ru_RU&apikey=d9168899-cf24-452a-95cf-06d7ac5a982b"
data = requests.get(url).json()

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

print(data["properties"]["ResponseMetaData"]["SearchRequest"]["boundedBy"])

Prints:

[[37.048427, 55.43644866], [38.175903, 56.04690174]]

CodePudding user response:

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)
...
  • Related