2022-04-23(토) TIL

황인호·2022년 6월 7일
0

TIL 모음

목록 보기
30/119
  • 오늘 해야할일

puppeteer 사용하여 크롤링해보기

MySQL공부하기(5주차까지 다들어보기)

소켓 공부 자료 https://poiemaweb.com/nodejs-socketio

Node.js 크롤링 개념 자료 https://bluayer.com/35

Node.js 크롤링 실전예제 자료! https://velog.io/@jinuku/Puppeteer를-이용한-웹-크롤링-해보기-예제-1

kakao 소셜로그인 https://velog.io/@nara7875/Node.js-kakao-login-api-가져오기

google 소셜 로그인https://uju-tech.tistory.com/entry/Nodejs-소셜로그인-정복하기-google

채팅봇 https://dev-dain.tistory.com/71

Node.js 크롤링

//먼저는 npm init -y  package.json 설치한다.
//그리고 npm i puppeteeer 을 설치해준다.

const puppeteer = require('puppeteer')
const cherrio = require('cherrio')
(async()=> {
	//브라우저를 실행한다.
	//옵션으로 headless 모드를 끌 수 있다.
	const browser = await puppeteer.launch({
		// true 는 인터넷 창을 열지않겠다!
		// false 는 인터넷 창을 열겠다!
		headless = false
	})
	
	//새로운 페이지를 연다
	//페이지의 크기를 설정한다.
	await page.setViewport({
		width:1366,
		height:768
	})
	//야놀자 사이트...ㅎㅎ
	//goto안에는 크롤링 하고자하는 사이트URL 입력
	await page.goto("https://www.goodchoice.kr/product/search/2")

	//페이지의 HTML을 가져온다.
	const content = await page.content()
	
	//$에 cheerio를 로드한다.
	const $ = cheerio.load(content)
	
	//복사한 리스트의 Selector로 리스트를 모두 가져온다.
	//#product_list_area > li:nth-child(2)
	const lists = $("#product_list_area > li")

	//모든 리스트를 순회한다.
	lists.each((index,list)=> {
		//호텔의 이름을 가져온다.
		//#poduct_list_area > li:nth-child(2) > a > div > div.name > strong
		const name = $(list).find('a > div > div.name > strong').text()
		
		//호텔의 가격을 가져온다.
    // #poduct_list_area > li:nth-child(2) > a > div > div.price > p > b
		const price = $(list).find('a > div > div.price > p > b').text()

		//호텔의 위치를 가져온다.
    // #poduct_list_area > li:nth-child(2) > a > div > div.name > p:nth-child(4)
		
		const location = $(list).find('a > div > div.name > p:nth-child(4)').text().slice(25,43)
		
		console.log({
			index,name,price,location
		})
	})
	browser.close()
})()

MySQL

Select 쿼리문의 개념

쿼리(Query)문이란? 쿼리는 질의를 의미합니다.

즉 데이터베이스에 명령을 내리는 것을 의미합니다.

여기서 Select 쿼리문은, 데이터베이스에서 ‘데이터를 선택해서 가져오겠다’는 의미입니다.

테이블과 필드는 뭘까요??

  • 이블: orders라는 엑셀 시트명 보이시죠? 테이블은 데이터가 담긴 엑셀 시트와 동일합니다. 이런 형태의 값이 데이터베이스에 담기면, orders라는 이름의 테이블이 되겠죠.
  • 필드: order_no, created_at, course_title, user_id, payment_method, email 각각이 필드입니다.
예시) Select 쿼리문을 통해 'orders 테이블의 created_at, course_title, payment_method, email 필드를 가져와줘!'라고 명령을 내릴 수 있답니다.
  • Where 절의 개념 Where 절은, Select 쿼리문으로 가져올 데이터에 조건을 걸어주는 것을 의미합니다.
  • Where 절과 같이 쓰는 문법
    1. 같지 않음
    • 같지 않음 조건은 ! = 로 걸 수 있습니다.
    1. 범위 조건
    • ex) 7월13일,7월14일의 주문데이터가 보고싶다.
    • between ‘2020-07-13’ and ‘2020-07-14’
    1. 포함 조건
    • 포함 조건은 in으로 걸수 있습니다.
    • ex) week in (1,3)
    1. 패턴(문자열 규칙) 조건
    • 패턴 조건은 like로 걸 수 있습니다.
    • ex) like %daum.net
profile
성장중인 백엔드 개발자!!

0개의 댓글