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"))
我只想從此串列中選擇兩個屬性,如“boundedBy”和“坐標”我該怎么做?我已經檢查了整個 bs 檔案,但沒有找到解決方案
uj5u.com熱心網友回復:
服務器回傳的結果是Json格式,所以使用json決議器或.json()方法對其進行解碼:
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"])
印刷:
[[37.048427, 55.43644866], [38.175903, 56.04690174]]
uj5u.com熱心網友回復:
使用.json()回應的方法,因為資料是 JSON。然后您可以迭代回應中的功能。請注意,您可以將引數設定為與 URL 分開,以便它們可讀且更易于更改:
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})')
輸出:
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)
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535234.html
下一篇:求分數n/1到1/n的總和
