[파이썬] 파이썬으로 웹 스크래퍼 만들기, Part 2 requests 모듈 설치 / Beautiful Soup 모듈 설치

미음제

·

2021. 2. 4. 22:04

파이썬으로 웹 스크래퍼 만들기

 

Part 1 이론

Part 2 파이썬으로 웹 스크래퍼 만들기

Part 3 Django를 사용하기 위해 알면 좋은 것

 

파이썬으로 웹 스크래퍼 만들기는 위와 같은

3개의 Part로 나누어 진행한다.

 

이 전 글 보기

2021/02/04 - [Developer/Python] - [파이썬] 파이썬으로 웹 스크래퍼 만들기, Part 2 웹 스크래핑이란


Part 2 파이썬으로 웹 스크래퍼 만들기

 

웹 스크래퍼를 만들기 위한

단계는 다음과 같다.

 

1. url로 접근(python을 통해)

2. 페이지 수 파악, 각 페이지에 하나씩 들어가기

(검색 조건, 몇 개가 검색될지 설정)

3. indeed 정보 추출 후 stackoverflow 추출

(번갈아가며 x)

4. 가져온 정보 엑셀 시트에 정리

 

 

정보를 추출하기위해

라이브러리나 모듈을 사용할 수 있는데

requests라는 모듈을 사용한다.

 

Requests

requests.readthedocs.io/en/master/

 

Requests: HTTP for Humans™ — Requests 2.25.1 documentation

Requests: HTTP for Humans™ Release v2.25.1. (Installation) Requests is an elegant and simple HTTP library for Python, built for human beings. Behold, the power of Requests: >>> r = requests.get('https://api.github.com/user', auth=('user', 'pass')) >>> r.

requests.readthedocs.io

 

Requests 설치 방법

python -m pip install requests

 

 

 

 

 

 

requests를 사용하는 방법은

다음에서 자세히 살펴볼 수 있다.

github.com/psf/requests

 

psf/requests

A simple, yet elegant HTTP library. Contribute to psf/requests development by creating an account on GitHub.

github.com

웹 스크래퍼를 만들기 위해서

requests를 import 한 후

결과를 저장할 변수를 선언한다.

 

import requests

결과를 담을 변수 = requests.get('복사한 url')

.get() 함수는

requests의 object안에 있는

function이다.

 

indeed에서 정보를

추출해야 하기 때문에

다음과 같이 코드를 작성한다.

import requests

indeed_result = requests.get(
    'https://kr.indeed.com/%EC%B7%A8%EC%97%85?q=%ED%8C%8C%EC%9D%B4%EC%8D%AC&limit=50')

그리고 print를 해보면

추출한 정보를 확인할 수 있다.

 

print(indeed_result.text)

이렇게 결과 뒤에 .text를 붙여주면

그 결과의 html 전체를 가져온다.

 

여기서 추출한 html에서

정보를 추출하는 것이다.

 


 

 

Beautiful Soup

www.crummy.com/software/BeautifulSoup/bs4/doc/

 

Beautiful Soup Documentation — Beautiful Soup 4.9.0 documentation

Non-pretty printing If you just want a string, with no fancy formatting, you can call str() on a BeautifulSoup object (unicode() in Python 2), or on a Tag within it: str(soup) # ' I linked to example.com ' str(soup.a) # ' I linked to example.com ' The str(

www.crummy.com

Beautiful Soup은

html에서 정보를 추출하기 위한

package이다.

 

Beautiful Soup 설치방법

pip install beautifulsoup4

 


스크래퍼를 만들기 위한

모듈과 패키지는 설치 완료했다.

 

다음엔 우리의 코드가

정보를 추출해 올 url에서

몇 개의 페이지가 있는지

알 수 있게 해주도록 한다.

 

 

반응형