hello everyone! Here is a record of a non professional Xiaobai's path to learn python, which is completely zero basic self-study. As the author just graduated from college, he doesn't like the job he found in his major, has a long free time, and studies Python on the job. He wants to use this platform to record his learning path. I hope the leaders can actively point out any mistakes in the article. Thank you very much! I hope to learn and make progress with you. Thank you. Book based Python Programming: from introduction to practice Author: Eric Matthes [beauty]
1, Review yesterday's study
The main content of yesterday's study was to review the content of the previous chapter. The last chapter mainly learned how to draw the temperature chart using csv format file and draw the world population map using json format file. The following two are the effect pictures. If you want to know more about these contents, you can pay attention to the author and read the author's study diary on October 345 for common learning.
2, New learning today
Today, I mainly study the program interaction of web API to obtain data and use URL to request specific information. What is a URL? It refers to the locator for obtaining unified resources. Each file we download on the website has its own independent URL. Today we will use the API to get GitHub( https://github.com/ )Data in.
If you want to learn more about github, you can watch the following paragraph. Of course, you can skip it.
GitHub( https://github.com/ )Git is a distributed version control system that allows teams of programmers to collaborate on projects. Git helps you manage the work you do for the project so that changes made by one person do not affect changes made by others. When you implement new features in your project, GIT will track the changes you make to each file. Once you've determined that the code works, you submit your changes, and git records the latest status of the project. If you make a mistake and want to undo your changes, you can easily return to any previous feasible state (for a more in-depth understanding of how to use git for version control, see Appendix D). Projects on GitHub are stored in a repository that contains everything associated with the project: code, information about project participants, problem or bug reports, and so on. For favorite projects, GitHub users can star them to show support, and users can also track the projects they may want to use. In this chapter, we will write a program that automatically downloads and visualizes the information of the highest star Python project on GitHub.
1. What is the data called by API? We input https://api.github.com/search/repositories?q=language:python&sort=stars The following set of data can be obtained in the browser:
2. Install requests
Enter pip instal -- user requests in the terminal. The following figure shows that the installation is successful.
3. Process the API response and store the results in the dictionary , And process the dictionary to outline the most popular warehouses. Please see the following code for details, which are explained in detail.
Operation effect diagram:
Program source code:
#coding=gbk #python_repos.py import requests #Execute the API call and store the response #Get uniform resource locator url = 'https://api.github.com/search/repositories?q=language:python&stats' r = requests.get(url) print("Status code:",r.status_code)#Verify whether the request is successful. 200 indicates success #Store the API response in a variable response_dict = r.json()#Using json format to convert data into json dictionary print("Total repositories:",response_dict['total_count'])#Indicate how many python repositories the github contains in total #Explore information about the warehouse repo_dicts = response_dict['items'] print("Respositories returned:",len(repo_dicts))#Indicates the length of the obtained list #Study the first warehouse repo_dict = repo_dicts[0]#Extract the first warehouse for research #print("\nKeys:",len(repo_dict))#Number of print dictionary keys #for key in sorted(repo_dict.keys()): # print(key)#Traverse all keys and print print("\nSelected information about first repository:") for repo_dict in repo_dicts:#Traversal repo_ All dictionaries in dicts print("\nName:",repo_dict['name'])#entry name print("Owner:",repo_dict['owner']['login'])#Project owner print("Stars:",repo_dict['stargazers_count'])#Stars obtained print("Repository:",repo_dict['html_url'])#url print("Created:",repo_dict['created_at'])#Project creation time print("Updated:",repo_dict['updated_at'])#Last update time print("Description:",repo_dict['description'])
Tomorrow we will continue to learn how to visualize the warehouse using pygal.
If you want to know more or have any questions, please leave a message or pay attention to the author, and give more praise and support. Your viewing praise is the biggest reward and motivation for me. Thank you!