@eDoNe

Как исправить ошибку 'NoneType' object has no attribute 'get'?

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'
  • Вопрос задан
  • 227 просмотров
Пригласить эксперта
Ответы на вопрос 1
@MagM1go
Это значит ошибка в парсинге. Попробуй подобрать другие классы и прочее. Потому что тут означает, что тебе возвращается объект None, думаю - логично
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы