Nội dung buổi Tutorial ngày hôm nay
Buổi hôm nay mình có cover các nội dung như sau: - How to get data from sharepoints? Link tham khảo https://pypi.org/project/Office365-REST-Python-Client/ import json from office365.runtime.auth.user_credential import UserCredential from office365.sharepoint.request import SharePointRequest site_url = "https://{your-tenant-prefix}.sharepoint.com" request = SharePointRequest(site_url).with_credentials(UserCredential("{username}", "{password}")) response = request.execute_request("web") json = json.loads(response.content) web_title = json['d']['Title'] print("Web title: {0}".format(web_title)) Rồi từ json data, tạo dataframe như bình thường: df = pd.DataFrame([json]) - How can we get the data from SQL server? Có nhiều cách, đơn giản nhất thì dùng pyocdb https://www.codecademy.com/resources/docs/python/sql-connectors/pyodbc import pyocdb # Connect to the database connection = pyodbc.connect("Driver={Driver_Name};" "Server=server_name;" "Database=database_name;" "UID=user_id;" "PWD=password;") # Create a cursor object cursor = connection.cursor() # Execute a query cursor.execute("SQL QUERY") # Fetch results rows = cursor.fetchall() # Process results for row in rows: print(row) --------> Từ each row tạo dataframe như bình thường # Close the connection connection.close() Cách clean hơn là dùng SQLAlchemy dành cho các bạn muốn nâng cao. Nhưng phải hiểu về class: SQLAlchemy: https://docs.sqlalchemy.org/en/20/orm/quickstart.html - How can we get the data from an API? Link tham khảo: https://realpython.com/python-api/ import requests response = requests.get("https://api.thecatapi.com/") pd.DataFrame([response.text]) ----> Chuyển thành dataframe rồi làm thao tác như bình thường. Nếu data update liên tục thì có thể schedule Python script