Structuring the Requirements First
This assignment had clear requirements. It was five steps: collect fintech news titles via the Naver API, save to MySQL, save to CSV, turn the code into functions, and modularize. Even the deliverables were fixed: the collection file, the DB module, and a result screenshot. I wrote the code with reuse in mind from the start.
Reviewing the Basics of requests
import requests
r = requests.get(url, params=payload, headers=headers)
print(r.status_code) # 200 OK / 400, 500 error
response = r.json()r.textis encoded as utf-8, so Korean renders wellr.contentis the raw bytes the server sent, used for HTML parsingr.json()auto-converts via the internal json module
The Naver API requires X-Naver-Client-Id and X-Naver-Client-Secret in the headers for auth. I learned that every API has its own conventions for this.
Functions, Then Modules
To avoid repeating the same logic, I wrapped collection and saving in functions and split shared functions into a separate file, myfunc.
import sys
sys.path.append("../../")
from myfunc import ...Loading .env by explicit path (load_dotenv(path)) and keeping API keys out of the code carried directly over from earlier learning.
Takeaway
Key point
I felt the difference between a "write-once, throw-away script" and a "module." Once I wrapped things in functions and split them into files, I could pull the same tool back out for the next assignment, collecting Seoul real-estate transaction data. The functions and classes from Python basics became real tools here.