함수문법은 언제쓰는가??
자주 쓰는 문법이 있을 때
함수 잘 쓰는 법 1 : 긴 코드를 짧은 단어로 축약
def 인사하기():
print('안녕하세요 중고차딜러 차은우입니다.')
인사하기()
함수 잘 쓰는 법 2 : 마법의 모자 만들기 (구멍에 123 넣어서 print 해줘)
def 모자(구멍):
print(구멍)
모자(123)
함수 잘 쓰는 법 3 : 함수를 실행하고 나서 가죽을 남기고 싶을 때 (옵션일 뿐)
def 함수():
return 10
print(함수())
주식 현재가 가져오는 긴 함수 만들기 + txt파일에 저장하기
import requests
from bs4 import BeautifulSoup
def 현재가(구멍):
data = requests.get(f'https://finance.naver.com/item/sise.naver?code={구멍}')
soup = BeautifulSoup(data.content, 'html.parser')
print(soup.find_all('strong', id="_nowVal")[0].text)
return soup.find_all('strong', id="_nowVal")[0].text
종목들 = ['005930', '066575', '005380', '035720', '034220', '003490']
f = open('a.txt', 'w')
for i in 종목들:
f.write('\n'+ 현재가(i))
f.close()
1. 페이지가 나뉜 사이트
2. 무한스크롤이 되는 페이지 데이터 수집
개발자도구에서 Network 들어가서 돋보기 눌러서 원하는 게시물의 글자 검색
해당 url 요청
제목의 태그 찾기 = a태그의 class title_link 찾기
import requests
from bs4 import BeautifulSoup
data = requests.get('https://s.search.naver.com/p/review/46/search.naver?where=view&api_type=11&start=31&query=%EC%82%AC%EA%B3%BC&nso=&nqx_theme=%7B%22theme%22%3A%7B%22main%22%3A%7B%22name%22%3A%22food_ingredient%22%7D%7D%7D&ac=0&aq=0&spq=0&nx_search_query=&nx_and_query=&nx_sub_query=&prank=33&sm=tab_jum&ssc=tab.view.view&ngn_country=KR&lgl_rcode=09380106&fgn_region=&fgn_city=&lgl_lat=37.614165&lgl_long=126.920813&abt=&_callback=viewMoreContents')
soup = BeautifulSoup(data.text.replace('\\',''), 'html.parser')
글리스트 = soup.select('a.title_link')
print(글리스트[0].text)
print(글리스트[1].text)
print(글리스트[2].text)
해당 게시글의 제목 추출 가능
text 말고 ['href']로 바꾸면 해당 url 추출가능
import requests
from bs4 import BeautifulSoup
data = requests.get('https://s.search.naver.com/p/review/46/search.naver?where=view&api_type=11&start=31&query=%EC%82%AC%EA%B3%BC&nso=&nqx_theme=%7B%22theme%22%3A%7B%22main%22%3A%7B%22name%22%3A%22food_ingredient%22%7D%7D%7D&ac=0&aq=0&spq=0&nx_search_query=&nx_and_query=&nx_sub_query=&prank=33&sm=tab_jum&ssc=tab.view.view&ngn_country=KR&lgl_rcode=09380106&fgn_region=&fgn_city=&lgl_lat=37.614165&lgl_long=126.920813&abt=&_callback=viewMoreContents')
soup = BeautifulSoup(data.text.replace('\\',''), 'html.parser')
글리스트 = soup.select('a.title_link')
print(글리스트[0]['href'])
print(글리스트[1]['href'])
print(글리스트[2]['href'])
query가 검색어이고 start가 게시물 시작 번호이며
start = 1 에 query = 귤로 해서 검색어가 귤인 게시물 1번부터 10번까지 반복문으로 불러오면
import requests
from bs4 import BeautifulSoup
data = requests.get('https://s.search.naver.com/p/review/46/search.naver?where=view&api_type=11&start=1&query=귤&nso=&nqx_theme=%7B%22theme%22%3A%7B%22main%22%3A%7B%22name%22%3A%22food_ingredient%22%7D%7D%7D&ac=0&aq=0&spq=0&nx_search_query=&nx_and_query=&nx_sub_query=&prank=33&sm=tab_jum&ssc=tab.view.view&ngn_country=KR&lgl_rcode=09380106&fgn_region=&fgn_city=&lgl_lat=37.614165&lgl_long=126.920813&abt=&_callback=viewMoreContents')
soup = BeautifulSoup(data.text.replace('\\',''), 'html.parser')
글리스트 = soup.select('a.title_link')
for i in range(10):
print(글리스트[i].text)
아래와 같은 결과를 얻을 수 있다.
실제 결과와 같은 것을 볼 수 있다.
'공부 > Python(FastAPI)' 카테고리의 다른 글
코딩애플 python(2) (1) | 2023.12.21 |
---|---|
코딩애플 python(1) (1) | 2023.12.20 |
[노마드코더] while 및 if문 사용한 게임 만들기 (0) | 2023.10.03 |
[노마드코더] Python 기초 함수 만들기 (0) | 2023.10.03 |
FastAPI 기초 익히기 (0) | 2023.09.12 |
댓글