Python 기초 | html 파싱?

원준·2023년 5월 23일

파이썬

목록 보기
14/21

1. crwaling 이란?

  • 데이터 수집 -> 데이터 분석/처리 => 인공지능 모델 학습 => ...

2. Http 란?

  • hypertext transfer protocol

    요청응답
    requestresponse
    clientserver

3. html 파싱

  • hypertext markup language

  • 외부 라이브러리 이므로 설치 해야한다.

  • pip install requests

    import requests
    
    url = "https://www.naver.com/"
    response = requests.get(url)
    status = response.status_code
    html = response.text
    print(status) # 200

4. http 상태코드

코드내용
200요청 성공
302다른페이지로 바로 연결
400Bad Request / 요청 잘못됨
401Unauthorized / 비인증
403Forbidden / 접근 권한 없음
404Not Found / 경로를 찾을수 없음 / 요청 받은 내용이 없음
405Method Not Allowed / 접근 방법 잘못됨
500Internal Server Error / 서버 에러
501Not Implemented
502Bad Gateway / 잘못된 응답

5. url 구조

프로토콜://호스트주소:포트번호/경로?쿼리

http://naver.com/?~~~

  • ? 뒤에 오는 값들을 Query라고 한다.

6. BeautifulSoup

  • pip install BeautifulSoup4
  • html 파싱 (parsing)
  • 기본 String으로 값을 가져오는 것을 객체로 가져올 수 있도록 도와준다.
    from bs4 import BeautifulSoup
    html  = "<html><body>Hello</body></html>"
    soup = BeautifulSoup(html, "html.parser")
    print(soup.body) # <body>Hello</body>
    print(soup.body.text) # Hello
profile
공부해보자

0개의 댓글