Security Engineer 2022. 11. 21. 21:00

5.6 Job Posts

 

웹사이트의 job 정보를 가져 오려 할 때

list 의 각 항목에 대한 코드를 실행할 때는 무엇을 써야할까? ( for 문을 사용해야 한다. )

일단 jobs 클래스를 가진 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')
    jobs = soup.find_all('section', class_="jobs")
    print(len(jobs))

출력값:

2

 

class 가 jobs 인 section 은 총 2개이다.

 

전체 HTML 코드에서 class 가 jobs 인 section 안에 ul 안의 모든 li 를 출력하려 한다.

 

ul 안에 있는 모든 li 안의 각 항목을 코드로 실행하려 한다.

for 문을 사용해서 li 을 찾는 코드를 작성한다.

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 = soup.find_all('section', class_="jobs")
    for job_section in jobs:
        print(job_section.find_all('li'))

출력값:

 

이제  job의 모든 post들을 찾는다.

job post 는 list의 li들 이다.

list 안의 코드를 실행할 때는 for 문을 사용해서 작성한다.

///////////////////// 를 구분자로 사용한다.

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 = soup.find_all('section', class_="jobs")
    for job_section in jobs:
        job_posts = job_section.find_all('li')
        for post in job_posts:
            print(post)
            print("//////////////////////////")

출력값:

soup 라는 변수에 페이지의 모든 html 이 담긴 beautifulsoup 를 넣었다. (beautifulsoup 를 사용해서 가져온 html 코드들)

jobs class 를 가지고 있는 모든 section 을 찾았다.

section

이 class 를 가지고 있는 section 은 2개가 있는데,

사용자는 안으로 가서 모든 li 들을 찾는다.

그리고 li list 안에 있는 각 li 들을

구분자(//////)를 붙여 각각 print 했다.

이제 여기서 view-all class 를 가지고 있는 li 를 가지고 왔는데,

View all 버튼

이 li 는 실질적으로 필요하지 않은 li 라 제외해줘야한다.

 

beuatifulsoup는 모든 html text 를 실제 python 데이터 구조로 바꿔준다.

soup를 만들고 jobs class 를 가진 모든 section 들을 가져온다.

이 데이터들을 jobs list 에 넣었다.

soup = BeautifulSoup(response.text,'html.parser')
jobs = soup.find_all('section', class_="jobs")

job_posts 라는 list 는 python list 와 같으니까 list 에서 마지막 항목을 제거할 수 있다.

 soup = BeautifulSoup(response.text,'html.parser')
    jobs = soup.find_all('section', class_="jobs")
    for job_section in jobs:
        job_posts = job_section.find_all('li')
        for post in job_posts:
            print(post)
            print("//////////////////////////")

pop 이라는 메서드를 사용해서 리스트의 마지막 항목을 제거한다.

job_post.pop(-1) → 인덱스 기본 순서는 0,1,2,3 / 반대 순서는 -1,-2,-3 순서 이다. ( 마지막이니까 -1 사용 )

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 = soup.find_all('section', class_="jobs")
    for job_section in jobs:
        job_posts = job_section.find_all('li')
        job_posts.pop(-1)
        for post in job_posts:
            print(post)
            print("//////////////////////////")

출력값:

view-all class 를 가지고 있는 li 는 사라지고 필요한 li 만 가져와서 출력하게 되었다.