본문 바로가기
반응형

파이썬11

[파이썬] SQLite DB를 만들고 검색해보자! 데이터베이스 import sqlite3 # Function to create a new database def create_database(): # Connect to or create the database file conn = sqlite3.connect('example.db') # Create a cursor object to interact with the database cursor = conn.cursor() # Create a table cursor.execute('''CREATE TABLE IF NOT EXISTS records (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)''') # Commit the changes and close .. 2023. 5. 22.
[python]현재파일 경로 지정 chromedriver 가 항상 말썽이다. 왜 같은 폴더에 넣어도 cd = "chromedriver" 이 코드로 실행이 되지 않는지 모르겠다. import os cd = "{}\chromedriver".format(ospath.dirname(os.path.realpath(__file__))) 이렇게 넣어야만 같은 폴더안에 넣은 chromedirver.exe 파일이 실행이 잘된다. 세팅의 문제일 수 있지만, 배포의 문제도 있기 때문에 위와 같은 코드로 프로그램을 만드는 것이 좋을 듯 싶다. 2020. 4. 1.
[python]팟캐스트 크롤링 및 다운로드를 해보자~!! 일단 필요한 패키지를 pip이나 pip3를 이용해서 install 해준다. 1. pip3 install requests 2. pip3 install bs4 3. pip3 install itertools import requests from bs4 import BeautifulSoup from urllib.parse import urljoin from itertools import count from time import sleep def get_list(pid): for page in count(1): page_url = "http://www.podbbang.com/podbbangchnew/episode_list?" params = {'id' : pid, 'page' : page} print('page .. 2020. 3. 20.
[python]텍스트 파일 특정 줄 삭제(사이즈 줄이기) 후 저장 로그를 기록을 했는데, 너무 많은 정보가 찍혀 있어서...줄일 필요가 생겼다. 그래서 파이썬 문법도 잘 기억나지 않는데 여기저기 찾아보면서 만들었다. fr = open('text_원본.txt', 'r') fw = open('text_수정.txt', 'w') for i, line in enumerate(fr): if i % 4 == 0: line = fr.readline() fw.write(line) fr.close() fw.close() 4줄이 지날 때마다 한번씩 다른 파일에 한줄을 저장해 주는 코드이다. 이 몇줄의 코드를 짜기 위해서 아침 30분을 검색했다. 공부를 더 열심히 해야겠다는 생각이 들었다. 2020. 3. 18.
반응형