尝试使用底层 API。在过滤后检查实际的 API 调用将为您提供正确的 URL。
import requests
url = "https://www.99acres.com/api-aggregator/project/searchWidget?area_unit=1&platform=DESKTOP&moduleName=GRAILS_SRP&workflow=GRAILS_SRP&city=45&preference=S&res_com=R&page=1&page_size=10&isCrossSell=false"
data = requests.get(url=url).json()
print(data['newProjects'][1]['propTypeStr'])
# 2 BHK Apartment
过滤会改变 URL 的参数,例如:
https://www.99acres.com/api-aggregator/srp/search?bedroom_num=3&budget_min=136&locality_array=6046%2C6038&area_min=1900&area_unit=1&localityNameMap=%5Bobject%20Object%5D&platform=DESKTOP&moduleName=GRAILS_SRP&workflow=GRAILS_SRP&page_size=30&page=1&city=45&preference=S&res_com=R&seoUrlType=DEFAULT
这可以分解为urllib
:
from urllib import parse
url = "https://www.99acres.com/api-aggregator/srp/search?bedroom_num=3&budget_min=136&locality_array=6046%2C6038&area_min=1900&area_unit=1&localityNameMap=%5Bobject%20Object%5D&platform=DESKTOP&moduleName=GRAILS_SRP&workflow=GRAILS_SRP&page_size=30&page=1&city=45&preference=S&res_com=R&seoUrlType=DEFAULT"
parse.parse_qs(parse.urlparse(url).query)
# {'bedroom_num': ['3'],
# 'budget_min': ['136'],
# 'locality_array': ['6046,6038'],
# 'area_min': ['1900'],
# 'area_unit': ['1'],
# 'localityNameMap': ['[object Object]'],
# 'platform': ['DESKTOP'],
# 'moduleName': ['GRAILS_SRP'],
# 'workflow': ['GRAILS_SRP'],
# 'page_size': ['30'],
# 'page': ['1'],
# 'city': ['45'],
# 'preference': ['S'],
# 'res_com': ['R'],
# 'seoUrlType': ['DEFAULT']}