프로젝트 일기 뉴스 크롤링 통합, 리엑트
문제1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | def fetch_data(list_of_lists): import re import requests from bs4 import BeautifulSoup # ConnectionError 방지 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"} # 변수 정의 titlesbuildingRegister = [] titlesregisteredCopy = [] titlesidentification = [] titlespowerOfAttorney = [] titlesCertificateThatDoesNotExist = [] contentsbuildingRegister = [] contentsregisteredCopy = [] contentsidentification = [] contentspowerOfAttorney = [] contentsCertificateThatDoesNotExist = [] list_of_lists = [buildingRegister, registeredCopy, identification, powerOfAttorney, CertificateThatDoesNotExist] titles_list_of_lists = [titlesbuildingRegister, titlesregisteredCopy, titlesidentification, titlespowerOfAttorney, titlesCertificateThatDoesNotExist] contents_list_of_lists = [contentsbuildingRegister, contentsregisteredCopy, contentsidentification, contentspowerOfAttorney, contentsCertificateThatDoesNotExist] for j, url_list in enumerate(list_of_lists): for url in url_list: original_html = requests.get(url, headers=headers) html = BeautifulSoup(original_html.text, "html.parser") #뉴스 제목 가져오기 title = html.select_one("div#ct > div.media_end_head.go_trans > div.media_end_head_title > h2") title = ''.join(str(title)) # html태그제거 pattern1 = '<[^>]*>' #뉴스 본문 가져오기 title = re.sub(pattern=pattern1, repl='', string=title) titles_list_of_lists[j].append(title) # 기사 텍스트만 가져오기 content = html.select_one("div#dic_area") # list합치기 content = ''.join(str(content)) content = re.sub(pattern=pattern1, repl='', string=content) # // flash 오류를 우회하기 위한 함수 추가 content = content.replace(pattern2, '') content = content.replace('\n', '') # 여기에서 '\n\n\n\n\n' 부분을 제거합니다. content = content.replace('\xa0', '') contents_list_of_lists[j].append(content) print("titlesbuildingRegister:", titlesbuildingRegister) print("titlesregisteredCopy:", titlesregisteredCopy) print("titlesidentification:", titlesidentification) print("titlespowerOfAttorney:", titlespowerOfAttorney) print("titlesCertificateThatDoesNotExist:", titlesCertificateThatDoesNotExist) print("contentsbuildingRegister:", contentsbuildingRegister) print("contentsregisteredCopy:", contentsregisteredCopy) print("contentsidentification:", contentsidentification) print("contentspowerOfAttorney:", contentspowerOfAttorney) print("contentsCertificateThatDoesNotExist:", contentsCertificateThatDoesNotExist) return titles_list_of_lists, contents_list_of_lists fetch_data(titles_list_of_lists) --------------------------------------------------------------------------- LocationParseError Traceback (most recent call last) File c:\Users\smhrd\anaconda3\envs\p38_yolov5\lib\site-packages\requests\models.py:434, in PreparedRequest.prepare_url(self, url, params) 433 try: --> 434 scheme, auth, host, port, path, query, fragment = parse_url(url) 435 except LocationParseError as e: File c:\Users\smhrd\anaconda3\envs\p38_yolov5\lib\site-packages\urllib3\util\url.py:397, in parse_url(url) 396 except (ValueError, AttributeError): --> 397 return six.raise_from(LocationParseError(source_url), None) 399 # For the sake of backwards compatibility we put empty 400 # string values for path if there are any defined values 401 # beyond the path in the URL. 402 # TODO: Remove this when we break backwards compatibility. | cs |
문제2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | def execute_query_and_get_precautions(query): from sqlalchemy import create_engine, text import pandas as pd HOSTNAME = 'project-db-stu.ddns.net' PORT = 3307 DATABASE = 'smhrd_A_5' USERNAME = 'smhrd_A_5' PASSWORD = 'smhrd5' # MySQL 연결 엔진 만들기 CONNECTION_STRING = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4' engine = create_engine(CONNECTION_STRING, echo=False, pool_recycle=3600, pool_pre_ping=True) # NULL 값을 제외하고 공백만 있는 행도 제거하는 SQL 쿼리를 작성합니다. query = text(f"SELECT {query} FROM newsSummary WHERE {query} IS NOT NULL AND {query} != '' ORDER BY RAND() LIMIT 1;") # engine.connect()를 사용하여 명시적으로 연결합니다. with engine.connect() as conn: summary_df = pd.read_sql(query, conn) # 선택한 데이터 프레임 출력 print(summary_df) import openai precautions = [] # 발급받은 API 키 설정 OPENAI_API_KEY = "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" # openai API 키 인증 openai.api_key = OPENAI_API_KEY # 모델 - GPT 3.5 Turbo 선택 model = "gpt-3.5-turbo" # 질문 작성하기 query = f"{query}기사내용에 부동산 계약시 주의사항 3가지로 요약해줘." # 메시지 설정하기 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": query} ] # ChatGPT API 호출하기 response = openai.ChatCompletion.create( model=model, messages=messages ) answer = response['choices'][0]['message']['content'] print(answer) execute_query_and_get_precautions(identification) --------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 execute_query_and_get_precautions(identification) NameError: name 'identification' is not defined | cs |
문제3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | (p38_yolov5) C:\Users\smhrd\modew-final-project>npm run start > final-project@0.1.0 start > react-scripts start node:internal/modules/cjs/loader:1078 throw err; ^ Error: Cannot find module 'cross-spawn' Require stack: - C:\Users\smhrd\modew-final-project\node_modules\react-dev-utils\crossSpawn.js - C:\Users\smhrd\modew-final-project\node_modules\react-scripts\bin\react-scripts.js at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15) at Module._load (node:internal/modules/cjs/loader:920:27) at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at Object.<anonymous> (C:\Users\smhrd\modew-final-project\node_modules\react-dev-utils\crossSpawn.js:10:18) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Module.require (node:internal/modules/cjs/loader:1141:19) { code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\\Users\\smhrd\\modew-final-project\\node_modules\\react-dev-utils\\crossSpawn.js', 'C:\\Users\\smhrd\\modew-final-project\\node_modules\\react-scripts\\bin\\react-scripts.js' ] } | cs |
문제4)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | from flask import Flask app = Flask(__name__) @app.route('/get_precautions') def get_precautions(keyword): from sqlalchemy import create_engine, text import pandas as pd import openai HOSTNAME = 'project-db-stu.ddns.net' PORT = 3307 DATABASE = 'smhrd_A_5' USERNAME = 'smhrd_A_5' PASSWORD = 'smhrd5' # MySQL 연결 엔진 만들기 CONNECTION_STRING = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4' engine = create_engine(CONNECTION_STRING, echo=False, pool_recycle=3600, pool_pre_ping=True) # keyword를 사용한 SQL 쿼리 작성 query = text(f"SELECT {keyword} FROM newsSummary WHERE {keyword} IS NOT NULL AND {keyword} != '' ORDER BY RAND() LIMIT 1;") # engine.connect()를 사용하여 명시적으로 연결합니다. with engine.connect() as conn: summary_df = pd.read_sql(query, conn) # 선택한 데이터 프레임 출력 print(summary_df) # 발급받은 API 키 설정 OPENAI_API_KEY = "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" # openai API 키 인증 openai.api_key = OPENAI_API_KEY # 모델 - GPT 3.5 Turbo 선택 model = "gpt-3.5-turbo" # 질문 작성하기 query = f"{summary_df}기사내용에 부동산 계약시 주의사항 3가지로 요약해줘." # 메시지 설정하기 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": query} ] # ChatGPT API 호출하기 response = openai.ChatCompletion.create( model=model, messages=messages ) answer = response['choices'][0]['message']['content'] return answer if __name__ == '__main__': app.run(host='0.0.0.0', port=8080, debug=False) ['https://n.news.naver.com/mnews/article/022/0003329149?sid=102'] ERROR: unknown command "python3.7" * Serving Flask app '__main__' * Debug mode: off 액세스 권한에 의해 숨겨진 소켓에 액세스를 시도했습니다 --------------------------------------------------------------------------- OSError Traceback (most recent call last) File c:\Users\smhrd\anaconda3\envs\p38_yolov5\lib\site-packages\werkzeug\serving.py:710, in BaseWSGIServer.__init__(self, host, port, app, handler, passthrough_errors, ssl_context, fd) 709 try: --> 710 self.server_bind() 711 self.server_activate()4 ['https://n.news.naver.com/mnews/article/022/0003329149?sid=102'] ERROR: unknown command "python3.7" * Serving Flask app '__main__' * Debug mode: off 액세스 권한에 의해 숨겨진 소켓에 액세스를 시도했습니다 --------------------------------------------------------------------------- OSError Traceback (most recent call last) File c:\Users\smhrd\anaconda3\envs\p38_yolov5\lib\site-packages\werkzeug\serving.py:710, in BaseWSGIServer.__init__(self, host, port, app, handler, passthrough_errors, ssl_context, fd) 709 try: --> 710 self.server_bind() 711 self.server_activate() | cs |
문제5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | from flask import Flask, request app = Flask(__name__) @app.route('/') def index(): return "Welcome to the real estate precautions app! Use the /get_precautions endpoint with a keyword to get precautions." @app.route('/get_precautions') def get_precautions(): keyword = request.args.get('keyword', 'identification') from sqlalchemy import create_engine, text import pandas as pd import openai HOSTNAME = 'project-db-stu.ddns.net' PORT = 3307 DATABASE = 'smhrd_A_5' USERNAME = 'smhrd_A_5' PASSWORD = 'smhrd5' # MySQL 연결 엔진 만들기 CONNECTION_STRING = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4' engine = create_engine(CONNECTION_STRING, echo=False, pool_recycle=3600, pool_pre_ping=True) # keyword를 사용한 SQL 쿼리 작성 query = text(f"SELECT {keyword} FROM newsSummary WHERE {keyword} IS NOT NULL AND {keyword} != '' ORDER BY RAND() LIMIT 1;") # engine.connect()를 사용하여 명시적으로 연결합니다. with engine.connect() as conn: summary_df = pd.read_sql(query, conn) # 선택한 데이터 프레임 출력 print(summary_df) # 발급받은 API 키 설정 OPENAI_API_KEY = "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" # openai API 키 인증 openai.api_key = OPENAI_API_KEY # 모델 - GPT 3.5 Turbo 선택 model = "gpt-3.5-turbo" # 질문 작성하기 query = f"{summary_df}기사내용에 부동산 계약시 주의사항 3가지로 요약해줘." # 메시지 설정하기 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": query} ] # ChatGPT API 호출하기 response = openai.ChatCompletion.create( model=model, messages=messages ) answer = response['choices'][0]['message']['content'] return answer,summary_df if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=False) Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application. | cs |
서버에러발생
시도
1.from flask import jsonify를 추가,반환 값에서 answer와 summary_df를 포함하는 JSON 객체를 생성
2.반환 값에서 summary_df.to_string() 대신 summary_df.to_string().encode('utf-8').decode()를 사용
3.
해결1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | def fetch_data(): import re import requests from bs4 import BeautifulSoup buildingRegister = [] registeredCopy = [] identification = [] powerOfAttorney = [] CertificateThatDoesNotExist = [] for key in search_results_dict.keys(): if key == '부동산 건축물대장 사기피해': # '부동산 건축물대장 사기피해' 키의 밸류 값만 buildingRegister 리스트에 추가 buildingRegister.extend(search_results_dict[key]) elif key == '부동산 등기부등본 사기피해': registeredCopy.extend(search_results_dict[key]) elif key == '부동산 신분증 사기피해': identification.extend(search_results_dict[key]) elif key == '부동산 위임장 사기피해': powerOfAttorney.extend(search_results_dict[key]) elif key == '후견등기사항 부존재증명서 피해': CertificateThatDoesNotExist.extend(search_results_dict[key]) # ConnectionError 방지 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"} # 변수 정의 titlesbuildingRegister = [] titlesregisteredCopy = [] titlesidentification = [] titlespowerOfAttorney = [] titlesCertificateThatDoesNotExist = [] contentsbuildingRegister = [] contentsregisteredCopy = [] contentsidentification = [] contentspowerOfAttorney = [] contentsCertificateThatDoesNotExist = [] list_of_lists = [buildingRegister, registeredCopy, identification, powerOfAttorney, CertificateThatDoesNotExist] titles_list_of_lists = [titlesbuildingRegister, titlesregisteredCopy, titlesidentification, titlespowerOfAttorney, titlesCertificateThatDoesNotExist] contents_list_of_lists = [contentsbuildingRegister, contentsregisteredCopy, contentsidentification, contentspowerOfAttorney, contentsCertificateThatDoesNotExist] for j, url_list in enumerate(list_of_lists): for url in url_list: original_html = requests.get(url, headers=headers) html = BeautifulSoup(original_html.text, "html.parser") #뉴스 제목 가져오기 title = html.select_one("div#ct > div.media_end_head.go_trans > div.media_end_head_title > h2") title = ''.join(str(title)) # html태그제거 pattern1 = '<[^>]*>' #뉴스 본문 가져오기 title = re.sub(pattern=pattern1, repl='', string=title) titles_list_of_lists[j].append(title) # 기사 텍스트만 가져오기 content = html.select_one("div#dic_area") # list합치기 content = ''.join(str(content)) content = re.sub(pattern=pattern1, repl='', string=content) # // flash 오류를 우회하기 위한 함수 추가 content = content.replace(pattern2, '') content = content.replace('\n', '') # 여기에서 '\n\n\n\n\n' 부분을 제거합니다. content = content.replace('\xa0', '') contents_list_of_lists[j].append(content) print("titlesbuildingRegister:", titlesbuildingRegister) print("titlesregisteredCopy:", titlesregisteredCopy) print("titlesidentification:", titlesidentification) print("titlespowerOfAttorney:", titlespowerOfAttorney) print("titlesCertificateThatDoesNotExist:", titlesCertificateThatDoesNotExist) print("contentsbuildingRegister:", contentsbuildingRegister) print("contentsregisteredCopy:", contentsregisteredCopy) print("contentsidentification:", contentsidentification) print("contentspowerOfAttorney:", contentspowerOfAttorney) print("contentsCertificateThatDoesNotExist:", contentsCertificateThatDoesNotExist) | cs |
requests 라이브러리에서 URL을 구문 분석하는 과정에서 발생합니다. 이 경우, 제공된 URL이 올바르지 않거나 잘못된 형식
코드리뷰
- 코드가 중복되는 부분이 많습니다. 예를 들어, 변수와 리스트의 선언이 비슷한 패턴을 가지고 있습니다. 이를 개선하려면 사전(dictionary)을 사용하여 코드를 더 간결하게 만들 수 있습니다.
- 웹 스크래핑 부분에서 BeautifulSoup를 사용하였습니다. 이것은 괜찮지만, 웹 스크래핑에는 다양한 라이브러리가 있으므로 Scrapy나 Selenium과 같은 다른 라이브러리를 살펴보는 것도 좋습니다.
- 크롤링 대상 URL이 정의되지 않은 상태입니다. 코드에서 사용되는 search_results_dict는 외부에서 정의되어야 하는 것으로 보입니다. 이를 개선하려면 함수의 인자로 전달하는 것을 고려해보세요.
- 결과를 출력하는 부분이 반복되고 있습니다. 이 부분 역시 반복문을 사용하여 간결하게 만들 수 있습니다.
해결2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | def execute_query_and_get_precautions(keyword): from sqlalchemy import create_engine, text import pandas as pd import openai HOSTNAME = 'project-db-stu.ddns.net' PORT = 3307 DATABASE = 'smhrd_A_5' USERNAME = 'smhrd_A_5' PASSWORD = 'smhrd5' # MySQL 연결 엔진 만들기 CONNECTION_STRING = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4' engine = create_engine(CONNECTION_STRING, echo=False, pool_recycle=3600, pool_pre_ping=True) # keyword를 사용한 SQL 쿼리 작성 query = text(f"SELECT {keyword} FROM newsSummary WHERE {keyword} IS NOT NULL AND {keyword} != '' ORDER BY RAND() LIMIT 1;") # engine.connect()를 사용하여 명시적으로 연결합니다. with engine.connect() as conn: summary_df = pd.read_sql(query, conn) # 선택한 데이터 프레임 출력 print(summary_df) # 발급받은 API 키 설정 OPENAI_API_KEY = "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" # openai API 키 인증 openai.api_key = OPENAI_API_KEY # 모델 - GPT 3.5 Turbo 선택 model = "gpt-3.5-turbo" # 질문 작성하기 query = f"{summary_df}기사내용에 부동산 계약시 주의사항 3가지로 요약해줘." # 메시지 설정하기 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": query} ] # ChatGPT API 호출하기 response = openai.ChatCompletion.create( model=model, messages=messages ) answer = response['choices'][0]['message']['content'] print(answer) # 함수 호출 예시 execute_query_and_get_precautions("검색어") | cs |
query라고 하닌깐 충돌나는듯 그래서 keyword를 써서 실행하닌깐 작동 잘됨
코드리뷰
- import 구문들이 함수 내부에 위치하고 있습니다. 이렇게 되면 함수가 호출될 때마다 모듈을 재import하는 문제가 발생할 수 있습니다. 따라서 성능을 향상시키기 위해 함수 외부에서 모듈을 import하시는 것을 추천합니다.
- 함수에서 사용하는 변수들이 하드 코딩되어 있습니다. 이러한 구조는 유지 보수에 어려움을 초래할 수 있습니다. 따라서 함수 인자로 전달하거나 설정 파일에서 불러오는 방식을 고려하시는 것이 좋습니다.
- 함수 이름인 execute_query_and_get_precautions은 동작을 명확하게 설명하지 않습니다. 더 명확한 이름으로 변경하는 것을 고려해 보세요.
해결3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | C:\Users\smhrd\modew-final-project>npm install cross-spawn changed 2 packages, and audited 1639 packages in 4s 242 packages are looking for funding run `npm fund` for details 6 high severity vulnerabilities To address all issues (including breaking changes), run: npm audit fix --force Run `npm audit` for details. C:\Users\smhrd\modew-final-project>npm start > final-project@0.1.0 start > react-scripts start (node:608) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option. (Use `node --trace-deprecation ...` to show where the warning was created) (node:608) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option. Starting the development server... Compiled with warnings. Warning (212:80) autoprefixer: end value has mixed support, consider using flex-end instead [eslint] src\case\C6.js Line 1:17: 'useState' is defined but never used no-unused-vars src\case\C7.js Line 1:17: 'useState' is defined but never used no-unused-vars src\view\Conditions.js Line 1:38: 'useRef' is defined but never used no-unused-vars | cs |
문제는 'cross-spawn' 모듈이 없어서 발생 이 모듈은 프로젝트의 node_modules 폴더에 있어야 함.
프로젝트 폴더에서 터미널을 열고, npm install cross-spawn을 실행하여 'cross-spawn' 모듈을 설치합니다.
'cross-spawn' 모듈이 정상적으로 설치된 후, 다시 npm start를 실행하여 프로젝트를 시작합니다.
해결4)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | from flask import Flask, request app = Flask(__name__) @app.route('/') def index(): return "Welcome to the real estate precautions app! Use the /get_precautions endpoint with a keyword to get precautions." @app.route('/get_precautions') def get_precautions(): keyword = request.args.get('keyword', 'identification') from sqlalchemy import create_engine, text import pandas as pd import openai HOSTNAME = 'project-db-stu.ddns.net' PORT = 3307 DATABASE = 'smhrd_A_5' USERNAME = 'smhrd_A_5' PASSWORD = 'smhrd5' # MySQL 연결 엔진 만들기 CONNECTION_STRING = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4' engine = create_engine(CONNECTION_STRING, echo=False, pool_recycle=3600, pool_pre_ping=True) # keyword를 사용한 SQL 쿼리 작성 query = text(f"SELECT {keyword} FROM newsSummary WHERE {keyword} IS NOT NULL AND {keyword} != '' ORDER BY RAND() LIMIT 1;") # engine.connect()를 사용하여 명시적으로 연결합니다. with engine.connect() as conn: summary_df = pd.read_sql(query, conn) # 선택한 데이터 프레임 출력 print(summary_df) # 발급받은 API 키 설정 OPENAI_API_KEY = "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" # openai API 키 인증 openai.api_key = OPENAI_API_KEY # 모델 - GPT 3.5 Turbo 선택 model = "gpt-3.5-turbo" # 질문 작성하기 query = f"{summary_df}기사내용에 부동산 계약시 주의사항 3가지로 요약해줘." # 메시지 설정하기 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": query} ] # ChatGPT API 호출하기 response = openai.ChatCompletion.create( model=model, messages=messages ) answer = response['choices'][0]['message']['content'] return answer if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=False) | cs |
코드1에서 문제는 get_precautions 함수에 keyword 인수를 직접 전달하려고 했습니다. 그러나 Flask에서 경로(route)로부터 인수를 얻으려면 경로 데코레이터 내에서 <variable_name>을 사용해야 합니다. 하지만 이 경우, request.args.get()을 사용하여 쿼리 문자열에서 키워드를 검색하는 것이 더 적합함
Flask의 request 객체를 사용하여 쿼리 매개 변수를 처리합니다.
이렇게 하면 사용자가 URL에 키워드를 전달할 수 있습니다.
코드2의 주요 변경 사항은 다음과 같습니다.
1.from flask import Flask, request를 사용하여 request 객체를 가져옵니다.
2.'@app.route('/get_precautions')' 아래에 있는 get_precautions 함수에서 인수로 keyword를 사용하지 않습니다.
3.함수 내에서 'keyword = request.args.get('keyword', 'identification')'를 사용하여 URL에서 키워드 매개 변수를 검색하고 기본값으로 'identification'을 설정합니다.
사용자가 URL에 키워드를 제공할 수 있고, 제공되지 않으면 기본 키워드 'identification'이 사용됩니다.
이 접근 방식은 Flask 웹 애플리케이션에서 쿼리 매개 변수를 처리하는 데 더 적합합니다.
해결5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | from flask import Flask, request import json app = Flask(__name__) @app.route('/') def index(): return "Welcome to the real estate precautions app! Use the /get_precautions endpoint with a keyword to get precautions." @app.route('/get_precautions') def get_precautions(): keyword = request.args.get('keyword', 'identification') from sqlalchemy import create_engine, text import pandas as pd import openai HOSTNAME = 'project-db-stu.ddns.net' PORT = 3307 DATABASE = 'smhrd_A_5' USERNAME = 'smhrd_A_5' PASSWORD = 'smhrd5' # MySQL 연결 엔진 만들기 CONNECTION_STRING = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4' engine = create_engine(CONNECTION_STRING, echo=False, pool_recycle=3600, pool_pre_ping=True) # keyword를 사용한 SQL 쿼리 작성 query = text(f"SELECT {keyword} FROM newsSummary WHERE {keyword} IS NOT NULL AND {keyword} != '' ORDER BY RAND() LIMIT 1;") # engine.connect()를 사용하여 명시적으로 연결합니다. with engine.connect() as conn: summary_df = pd.read_sql(query, conn) # 선택한 데이터 프레임에서 요약 텍스트 추출 summary_text = summary_df.iloc[0][keyword] # 발급받은 API 키 설정 OPENAI_API_KEY = "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" # openai API 키 인증 openai.api_key = OPENAI_API_KEY # 모델 - GPT 3.5 Turbo 선택 model = "gpt-3.5-turbo" # 질문 작성하기 query = f"{summary_text}기사내용에 부동산 계약시 주의사항 3가지로 요약해줘." # 메시지 설정하기 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": query} ] # ChatGPT API 호출하기 response = openai.ChatCompletion.create( model=model, messages=messages ) answer = response['choices'][0]['message']['content'] # 문자열을 생성하고 반환합니다. response_text = f"뉴스요약 : \n{summary_text}\n\n\n 주의할 점:\n {answer}" return app.response_class(response_text, content_type='text/plain; charset=utf-8') if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=False) | cs |
Flask의 jsonify 함수 대신 Python의 기본 json 라이브러리를 사용하여 JSON 객체를 생성하고 반환하여 문자열 인코딩 문제를 해결 그후에 너무 지저분하게 나와서JSON배열 변경