상세 컨텐츠

본문 제목

[Python] 스크래핑 배우기 / Beautifulsoup4 / requests

Python

by TUZA 2024. 1. 22. 21:38

본문

BeautifulSoup

 

BeautifulSoup는 html 코드 내부를 검색할 수 있게 도와주는 패키지이다.

 

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#quick-start

 

Beautiful Soup Documentation — Beautiful Soup 4.12.0 documentation

Beautiful Soup Documentation Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers h

www.crummy.com

 

requests.get으로 원하는 홈페이지에 접속을 하고 

200 을 응답받으면 접속성공.

import requests 
from bs4 import BeautifulSoup

url = "https://weworkremotely.com/categories/remote-full-stack-programming-jobs#job-listings"

response = requests.get(url)

# print(response.status_code) : 해당 페이지의 접속상태를 알려줌

# print(response.content) #html source
soup = BeautifulSoup(response.content,"html.parser",)

print(soup.prettify())

#class를 찾을 때는 언더 바 (_) 사용. 왜냐하면 파이썬에서 class는 예약어 역할을 하기 때문이다.
jobs = soup.find("section",class_ = "jobs").find_all("li")

 

requests 활용 방법

 

#response.request

: 내가 보낸 request 객체에 접근이 가능하다.

 

#reponse.status_code

: 응답코드를 받을 수 있다.

 

#response.raise_for_status()

: 200 (OK 코드) 이 아닌 경우 에러 발생시킴.

 

#response.json()

: json response 일 경우 딕셔너리 타입으로 바로 변환

 

#reponse.content

: content 속성을 통해 바이너리 타입으로 데이터를 받을 수 있다.

 

#reponse.text

: text 속성을 통해 UTF-8로 인코딩된 문자열을 받을 수 있다.

 

#encoding 정보확인

: reponse.encoding

반응형

'Python' 카테고리의 다른 글

[Python] 스크래핑 배우기 2  (1) 2024.01.22
[Python] classes  (2) 2024.01.19
[Python] requests  (0) 2024.01.16
[Python] 딕셔너리(dictionary)  (0) 2024.01.16
[Python] data structure ( 데이터 구조 )  (2) 2024.01.16

관련글 더보기