Security Engineer 2022. 11. 17. 21:00

5.4 BeautifulSoup

 

아래 코드로 weworkremotely 웹사이트의 python 구인정보를 가져왔다.

from requests import get

base_url = "https://weworkremotely.com/remote-jobs/search?utf8=%E2%9C%93&term="

search_term = "python"

response = get(f'{base_url}{search_term}')
if response.status_code != 200:
    print("Can't request website")
else:
    print(response.text)

jobs 라는 class 를 가진 section 을 찾아 추출한다.

그리고 section 에 있는 ul 안에서 모든 li 를 찾는다.

많은 텍스트 안의 내용을 검색하기 위해 beautifulsoup 를 사용한다.

beautifulsoup 는 여러 method 들을 제공하는데

예를 들면 find_all 이라는 메서드가 있다.

사용자가 가진 document 에서 HTML 태그를 찾을 수 있게 해준다.

ex) soup.find_all("title")을 하면 title 이라는 HTML 태그를 가져온다.

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all

 

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, or on a Tag within it: str(soup) # ' I linked to example.com ' str(soup.a) # ' I linked to example.com ' The str() function returns a str

www.crummy.com

 

아래 코드로 beautifulsoup 를 사용한다.

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

html을 첫번째 argument로 전해주고 'html.parser'라는 문자열을 전해준다.

이건 사용자가 beautifulsoup 한테 html을 보내준다고 하는것과 같다.

 

이제 실제코드에 적용을 해서 나만의 soup를 만든다.

response 는 base_url + search_term 이 합쳐진 변수로 검색해서 나온 결과를 웹사이트로 보여준다.

get이라는 메서드로 그 웹사이트 검색 결과(HTML 코드들)를 response 라는 변수에 넣어

status_code(http 상태코드) 가 정상 접속이 아니면 Can't request webisite 라는 문구를 출력하고

status_code(http 상태코드) 가 정상이면 response 변수값을 text 형태로 출력하게 만들었다.

beautifulsoup 를 활용하여  

soup = BeautifulSoup(html_doc, 'html.parser')          → 예시

soup = BeautifulSoup(response.text,'html.parser')    → 실제 적용 코드

soup의 find_all 메서드를 사용해서 title 이라는 HTML 태그를 찾아서 출력했다.

from requests import get
from bs4 import BeautifulSoup

base_url = "https://weworkremotely.com/remote-jobs/search?utf8=%E2%9C%93&term="

search_term = "python"

response = get(f'{base_url}{search_term}')
if response.status_code != 200:
    print("Can't request website")
else:
    soup = BeautifulSoup(response.text,'html.parser')
    print(soup.find_all('title'))

출력값:

[<title>We Work Remotely: Advanced Remote Job Search</title>]

 

 

이제 실제로 찾을 타겟인 jobs라는 class를 가진 section 을 출력해본다.

from requests import get
from bs4 import BeautifulSoup

base_url = "https://weworkremotely.com/remote-jobs/search?utf8=%E2%9C%93&term="

search_term = "python"

response = get(f'{base_url}{search_term}')
if response.status_code != 200:
    print("Can't request website")
else:
    soup = BeautifulSoup(response.text,'html.parser')
    print(soup.find_all('section',class_="jobs"))

출력값:

 

검색한 웹사이트와 동일하게 가져왔는지 확인하기 위해

실제 웹사이트상의 맨 위의 구직 회사와 코드상의 회사 이름이 같은지 확인해본다.

위와 같이 일 정보가 있는 section 을 가져왔다.

이 결과물을 jobs 변수에 넣어둔다.

from requests import get
from bs4 import BeautifulSoup

base_url = "https://weworkremotely.com/remote-jobs/search?utf8=%E2%9C%93&term="

search_term = "python"

response = get(f'{base_url}{search_term}')
if response.status_code != 200:
    print("Can't request website")
else:
    soup = BeautifulSoup(response.text,'html.parser')
    jobs = print(soup.find_all('section',class_="jobs"))