如何使用LeetCode GraphQL查询提取数据

问题描述 投票:0回答:1

我只是想知道如何使用 GraphQL API 打印所有 JSON? 就像我想为我的项目提取 LeetCode 用户个人资料的所有数据,但我不知道如何编写查询来提取个人资料的所有数据。

我想从用户的 LeetCode 个人资料中提取一些特定的详细信息,但在此之前我想打印所有 JSON 数据,以便我可以编写特定的查询来提取特定的信息。 有没有办法打印所有数据,或者我可以在哪里学习在查询中写入什么内容以从用户的个人资料中提取特定信息。

我的主要编码语言是Python。

python json web-scraping python-requests graphql
1个回答
0
投票

我认为 @JonSG 对 graphql 做了足够的解释,所以我只是让您先了解如何使用 Graphql API 进行编码以从 JSON 获取所需的输出。

注意:我只是为了演示而添加了10页范围,leetcode在全球排名中有

22341
页的用户列表。

示例代码:

import requests
from string import Template

header = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0",
    "Content-Type": "application/json"
}
url = 'https://leetcode.com/graphql'
for i in range(1,10):
    val = 'query globalRanking {\n  globalRanking(page: $page) {\n    totalUsers\n    userPerPage\n    myRank {\n      ranking\n      currentGlobalRanking\n      currentRating\n      dataRegion\n      user {\n        nameColor\n        activeBadge {\n          displayName\n          icon\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    rankingNodes {\n      ranking\n      currentRating\n      currentGlobalRanking\n      dataRegion\n      user {\n        username\n        nameColor\n        activeBadge {\n          displayName\n          icon\n          __typename\n        }\n        profile {\n          userSlug\n          userAvatar\n          countryCode\n          countryName\n          realName\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n'
    s = Template(val)
    replace_page = s.substitute(page=i)
    data = {
    "operationName": "globalRanking",
    "variables": {},
    "query": f"{replace_page}"
    }
    response = requests.post(url, headers=header, json=data)
    for i in response.json()['data']['globalRanking']['rankingNodes']:
        data = [f"user_name: {i['user']['username']}", f"user_avatar: {i['user']['profile']['userAvatar']}", f"country: {i['user']['profile']['countryName']}", f"name: {i['user']['profile']['realName']}"]
        print(data)

示例输出:

['user_name: neal_wu', 'user_avatar: https://assets.leetcode.com/users/neal_wu/avatar_1574529913.png', 'country: United States', 'name: Neal Wu']
['user_name: Carefreejs', 'user_avatar: https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/users/endlesscheng/avatar_1690721039.png', 'country: ', 'name: 灵茶山艾府']
['user_name: numb3r5', 'user_avatar: https://assets.leetcode.com/users/default_avatar.jpg', 'country: Australia', 'name: Joshua Chen']
['user_name: liming-v', 'user_avatar: https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/users/heltion/avatar_1587213058.png', 'country: 中国', 'name: 何逊']
['user_name: ahmed007boss', 'user_avatar: https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/users/yawn_sean/avatar_1654149069.png', 'country: 中国', 'name: 小羊肖恩']
['user_name: hankray', 'user_avatar: https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/users/sserxhs/avatar_1622949152.png', 'country: 中国', 'name: SSerxhs']
['user_name: dnialh', 'user_avatar: https://assets.leetcode.com/users/avatars/avatar_1655848184.png', 'country: None', 'name: ']
['user_name: fjzzq2002', 'user_avatar: https://assets.leetcode.com/users/default_avatar.jpg', 'country: None', 'name: ']
['user_name: pandaforever', 'user_avatar: https://assets.leetcode.com/users/default_avatar.jpg', 'country: None', 'name: ']
['user_name: zhoupeiyun', 'user_avatar: https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/users/johnkram/avatar_1593402741.png', 'country: 中国', 'name: 汪乐平']
['user_name: getnaukri', 'user_avatar: https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/users/py-is-best-lang/avatar_1692179558.png', 'country: ', 'name: PyIsBestLang']
['user_name: jonathanirvings', 'user_avatar: https://assets.leetcode.com/users/default_avatar.jpg', 'country: Singapore', 'name: Jonathan Irvin Gunawan']
['user_name: xiaowuc1', 'user_avatar: https://assets.leetcode.com/users/default_avatar.jpg', 'country: United States', 'name: xiaowuc1']
['user_name: PurpleCrayon', 'user_avatar: https://assets.leetcode.com/users/rohingarg123/avatar_1584377597.png', 'country: None', 'name: Rohin Garg']

所有查询都将用作请求正文来提取特定的用户数据:

Public profile: {"query":"\n    query userPublicProfile($username: String!) {\n  matchedUser(username: $username) {\n    contestBadge {\n      name\n      expired\n      hoverText\n      icon\n    }\n    username\n    githubUrl\n    twitterUrl\n    linkedinUrl\n    profile {\n      ranking\n      userAvatar\n      realName\n      aboutMe\n      school\n      websites\n      countryName\n      company\n      jobTitle\n      skillTags\n      postViewCount\n      postViewCountDiff\n      reputation\n      reputationDiff\n      solutionCount\n      solutionCountDiff\n      categoryDiscussCount\n      categoryDiscussCountDiff\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"userPublicProfile"}    
Language Stats: {"query":"\n    query languageStats($username: String!) {\n  matchedUser(username: $username) {\n    languageProblemCount {\n      languageName\n      problemsSolved\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"languageStats"}    
Skills set: {"query":"\n    query skillStats($username: String!) {\n  matchedUser(username: $username) {\n    tagProblemCounts {\n      advanced {\n        tagName\n        tagSlug\n        problemsSolved\n      }\n      intermediate {\n        tagName\n        tagSlug\n        problemsSolved\n      }\n      fundamental {\n        tagName\n        tagSlug\n        problemsSolved\n      }\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"skillStats"}    
User Contest Ranking: {"query":"\n    query userContestRankingInfo($username: String!) {\n  userContestRanking(username: $username) {\n    attendedContestsCount\n    rating\n    globalRanking\n    totalParticipants\n    topPercentage\n    badge {\n      name\n    }\n  }\n  userContestRankingHistory(username: $username) {\n    attended\n    trendDirection\n    problemsSolved\n    totalProblems\n    finishTimeInSeconds\n    rating\n    ranking\n    contest {\n      title\n      startTime\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"userContestRankingInfo"}    
User question progress V2: {"query":"\n    query userProfileUserQuestionProgressV2($userSlug: String!) {\n  userProfileUserQuestionProgressV2(userSlug: $userSlug) {\n    numAcceptedQuestions {\n      count\n      difficulty\n    }\n    numFailedQuestions {\n      count\n      difficulty\n    }\n    numUntouchedQuestions {\n      count\n      difficulty\n    }\n    userSessionBeatsPercentage {\n      difficulty\n      percentage\n    }\n  }\n}\n    ","variables":{"userSlug":"TARGET USERNAME IS HERE"},"operationName":"userProfileUserQuestionProgressV2"
User question progress V1: {"query":"\n    query userSessionProgress($username: String!) {\n  allQuestionsCount {\n    difficulty\n    count\n  }\n  matchedUser(username: $username) {\n    submitStats {\n      acSubmissionNum {\n        difficulty\n        count\n        submissions\n      }\n      totalSubmissionNum {\n        difficulty\n        count\n        submissions\n      }\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"userSessionProgress"}    
User Badge: {"query":"\n    query userBadges($username: String!) {\n  matchedUser(username: $username) {\n    badges {\n      id\n      name\n      shortName\n      displayName\n      icon\n      hoverText\n      medal {\n        slug\n        config {\n          iconGif\n          iconGifBackground\n        }\n      }\n      creationDate\n      category\n    }\n    upcomingBadges {\n      name\n      icon\n      progress\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"userBadges"}    
User profile calendar: {"query":"\n    query userProfileCalendar($username: String!, $year: Int) {\n  matchedUser(username: $username) {\n    userCalendar(year: $year) {\n      activeYears\n      streak\n      totalActiveDays\n      dccBadges {\n        timestamp\n        badge {\n          name\n          icon\n        }\n      }\n      submissionCalendar\n    }\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE"},"operationName":"userProfileCalendar"}    
User recent submission: {"query":"\n    query recentAcSubmissions($username: String!, $limit: Int!) {\n  recentAcSubmissionList(username: $username, limit: $limit) {\n    id\n    title\n    titleSlug\n    timestamp\n  }\n}\n    ","variables":{"username":"TARGET USERNAME IS HERE","limit":15},"operationName":"recentAcSubmissions"}

请告诉我这是否是您想要的!

© www.soinside.com 2019 - 2024. All rights reserved.