Я взял код @Gautam и только перестроил его.
Первый запрос дает только 100 элементов (даже с пределом = 300), поэтому, используя start
, я получаю следующие 28 элементов.
Использование json=
вместо data=
Мне не нужноheaders=
и json.dump()
#!/usr/bin/env python
import requests
import warnings
warnings.filterwarnings("ignore")
def display(data):
#print('len:', len(data))
#print('len:', len(data[0]['data']['CatalogResultsV2Resource']['browseV2']['elements']))
print('>>> len:', len(data[0]['data']['CatalogResultsV2Resource']['browseV2']['elements'][0]['courses']['elements']))
items = data[0]['data']['CatalogResultsV2Resource']['browseV2']['elements'][0]['courses']['elements']
for item in items:
print(item['name'])
#for key, value in item.items():
# print(key, value)
#print('---')
#-----------------------------------------------------------
json_data = [{
'operationName': 'catalogResultQuery',
'variables': {
'skip': False,
'limit': 300,
'start': '0',
'sortField': '',
'facets': [
'skillNameMultiTag',
'jobTitleMultiTag',
'difficultyLevelTag',
'languages:English',
'entityTypeTag',
'partnerMultiTag',
'categoryMultiTag',
'subcategoryMultiTag:history'
]
},
'query': 'query catalogResultQuery($facets: [String!]!, $start: String!, $skip: Boolean = false, $sortField: String, $limit: Int) { CatalogResultsV2Resource { browseV2(facets: $facets, start: $start, limit: $limit, sortField: $sortField) @skip(if: $skip) { elements { label entries { id score courseId specializationId onDemandSpecializationId resourceName __typename } domainId subdomainId facets courses { elements { ...CourseFragment __typename } __typename } s12ns { elements { ...S12nFragment __typename } __typename } __typename } paging { total next __typename } __typename } __typename } } fragment CourseFragment on CoursesV1 { id slug name photoUrl s12nIds level workload courseDerivativesV2 { skillTags { skillName relevanceScore __typename } avgLearningHoursAdjusted commentCount averageFiveStarRating ratingCount __typename } partners { elements { name squareLogo classLogo logo __typename } __typename } __typename } fragment S12nFragment on OnDemandSpecializationsV1 { name id slug logo courseIds derivativeV2 { averageFiveStarRating avgLearningHoursAdjusted __typename } partners { elements { name squareLogo classLogo logo __typename } __typename } metadata { headerImage level __typename } courses { elements { courseDerivativesV2 { skillTags { skillName relevanceScore __typename } __typename } __typename } __typename } __typename } '
}]
url = 'https://www.coursera.org/graphqlBatch'
#headers = {'content-type': 'application/json'}
#r = requests.post(url, headers=headers, json=json_data, verify=False)
# --- it gives first 100 items ---
r = requests.post(url, json=json_data, verify=False)
data = r.json()
display(data)
# --- it gives next 28 items ---
json_data[0]['variables']['start'] = str(100) # it has to be string, not integer
r = requests.post(url, json=json_data, verify=False)
data = r.json()
display(data)
Начало результата:
>>> len: 100
Buddhism and Modern Psychology
English Composition I
Fashion as Design
The Modern World, Part One: Global History from 1760 to 1910
Indigenous Canada
Understanding Einstein: The Special Theory of Relativity
Terrorism and Counterterrorism: Comparing Theory and Practice
Magic in the Middle Ages
The Ancient Greeks
Introduction to Ancient Egypt and Its Civilization
Конец результата:
>>> len: 28
Theatre and Globalization
ART of the MOOC: Arte Público y Pedagogía
The Music of the Rolling Stones, 1962-1974
Soul Beliefs: Causes and Consequences - Unit 2: Belief Systems
The Making of the US President: A Short History in Five Elections
Cities are back in town : sociologie urbaine pour un monde globalisé
Toledo: Deciphering Secrets of Medieval Spain
Russia and Nuclear Arms Control
Espace mondial, a French vision of Global studies
Religious Transformation in Early China: the Period of Division
Patrick Henry: Forgotten Founder
A la recherche du Grand Paris
Burgos: Deciphering Secrets of Medieval Spain
Journey Conversations: Weaving Knowledge and Action
Structuring Values in Modern China
Religion and Thought in Modern China: the Song, Jin, and Yuan
宇宙之旅:展现生命 (Journey of the Universe: The Unfolding of Life)
The Worldview of Thomas Berry: The Flourishing of the Earth Community
Science and Technology in the Silla Cultural Heritage
世界空间、法国视角下的国���研究
Fundamentals of the Chinese character writing (Part 1)
Understanding China, 1700-2000: A Data Analytic Approach, Part 2
"Espace mondial" الرؤية الفرنسية للدراسات العالمية
Searching for the Grand Paris
宇宙之旅:对话 (Journey of the Universe: Weaving Knowledge and Action)
Contemporary India
Thomas Berry的世界观:地球社区的繁荣 (The Worldview of Thomas Berry: The Flourishing of the Earth Community)
"Making" Progress Teach-Out