import requests
import json
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0",
"bx-ajax": "true"
}
def get_page(url):
s = requests.Session()
response = s.get(url=url, headers=headers)
with open("index.html", "w") as file:
file.write(response.text)
def get_json(url):
s = requests.Session()
response = s.get(url=url, headers=headers)
with open("result.json", "w") as file:
json.dump(response.json(), file, indent=4, ensure_ascii=False)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # TEST # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def collect_test():
s = requests.Session()
response = s.get(url="https://ru.sportland.ee/graphql?hash=1665343408&_sort_0={newest_products:DESC}&_filter_0={min_price:{gteq:1},max_price:{lteq:27},category_id:{eq:1122},brand:{in:[Champion]}}&_pageSize_0=20&_currentPage_0=1", headers=headers)
data = response.json()
page_info = data.get("data").get("products").get("page_info").get("total_pages")
result_data = []
for page_count in range(1, page_info +1):
url = "https://ru.sportland.ee/graphql?hash=1665343408&_sort_0={newest_products:DESC}&_filter_0={min_price:{gteq:1},max_price:{lteq:27},category_id:{eq:1122},brand:{in:[Champion]}}&_pageSize_0=20&_currentPage_0=" + f"{page_count}"
r = s.get(url=url, headers=headers)
data = r.json()
products = data.get("data").get("products").get("items")
for pc in products:
maximalPrice = pc.get("price").get("maximalPrice").get("amount").get("value")
regularPrice = pc.get("price").get('regularPrice').get("amount").get("value")
discount = (1 - maximalPrice / regularPrice) * 100
if discount < 100 :
result_data.append(
{
"name": pc.get("name"),
"link": f'https://ru.sportland.ee/product/{pc.get("url_key")}',
"regularprice": pc.get("price").get("regularPrice").get("amount").get("value"),
"maximalprice": pc.get("price").get("maximalPrice").get("amount").get("value"),
"discount": int(discount),
"status": pc.get("variants").get("product").get("assigned_sources").get("status")
}
)
print(f"{page_count}/{page_info}")
with open("result_data.json", "w") as file:
json.dump(result_data, file, indent=4, ensure_ascii=False)
def main():
collect_test()
Другие данные находит, кроме последнего запроса
Выдает в консоли:
File "C:\Users\Родители\Documents\collect_test.py", line 57, in collect_test
"status": pc.get("variants").get("product").get("assigned_sources").get("name")
AttributeError: 'NoneType' object has no attribute 'get'