5.7 Job Extraction
https://weworkremotely.com/remote-jobs/search?utf8=%E2%9C%93&term=python
We Work Remotely: Advanced Remote Job Search
Advanced job search for We Work Remotely, allowing you to search and refine jobs across programming, marketing, customer service, etc. Find your next remote career.
weworkremotely.com
weworkremotely posts 에서 데이터를 추출해본다.
href 데이터를 추출한다. ( 링크를 저장하기 위해 ) - 결국 필요한건 링크에 대한 구인 정보다.
company 이름, job title, Full-time 유무, region 정보를 가져올것이다.
beautifulsoup으로 html 안에 있는 것들을 검색하는 기능 말고,
모든 html 태그를 beautifulsoup entity로 만드는 기능도 있다.
사용한 코드에서 class가 jobs인 section을 찾을 때
jobs 정보를 string으로 받을 수 있었지만, 그러지 않았다.
내가 받은건 find_all을 사용할 수 있는 job_section 이었다.
post 도 이와 같은 방식으로 동작한다.
job_posts가 section 안에 있는 모든 li의 list 라는걸 알 수 있지만, ( 이전 게시물에서 li를 가져왔다. )
fild_all을 사용할 수 있는 beautifulsoup entity이기도 한다.
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("//////////////////////////")
각 li 내부로 가서 각 section의 anchor를 찾을수 있다.
위에 anchor 는 가져오지 않고 ( 필요한 정보가 없다. )
밑에 필요한 anchor 의 링크를 가져와서 엑셀로 저장하려 한다. ( 회사 정보, 근무 형태 등 정보가 있다. )
li 에 있는 모든 anchor 를 찾아서 첫번째 anchor 는 제거하고 데이터를 가져와본다.
인덱스 기능을 이용해서 anchor[1] → anchor 의 두번째 데이터만 가져온다.
beautifulsoup는 html 태그들을 dictionary 처럼 바꿔서 python dictionary 형태로 쓸수 있게 해준다.
dictionary의 특징은 대괄호 [ ] 안에 key 를 넣어주면 출력된다.
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:
anchors = post.find_all('a')
anchor = anchors[1]
print(anchor['href'])
출력값:
link 라는 변수에 위 출력값들을 넣어둔다.
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:
anchors = post.find_all('a')
anchor = anchors[1]
link = anchor['href']
이제 anchor 안에 있는 span 에 접근해본다.
class 중에 맞지 않는 class 의 이름을 가지고 있는 class 들이 있다. → 다른 class 이름이 되야 한다.
company > Full-time
region company > Europe Only
python 에서 object 의 list를 가지고 있고 list의 길이를 알면 list에서 object를 가져오는 쉬운 코드가 있다.
만약 list_of_numbers 같은 array나 list를 가지고 있어 각 요소를 변수로 만들고 싶다면,
아래처럼 1개씩 변수에 넣을 수 도 있지만
list_of_numbers = [1,2,3]
first = list_of_numbers[0]
second = list_of_numbers[1]
third = list_of_numbers[2]
print(first,second,third)
이러기에는 불편하므로 아래 코드처럼 순서대로 맞게 넣어줄수도 있다.
전제 조건은 list의 길이를 알아야 한다.
list_of_numbers = [1,2,3]
first, second, third = list_of_numbers
print(first, second, third)
출력값:
1 2 3
find_all_company 를 하기위해 위의 코드를 응용해서 사용하면 편리하다고 생각한다.
class 에 coompany가 포함된 부분만 가져오게 되면
Soundsnap → company의 이름 ( company)
Full-Time → 근무 형태 ( kind )
Europe Only → 근무 지역 ( region )
위 세가지 부분을 가져올 수 있다.
위에서 list 길이를 알면 object 를 쉽게 가져오는 코드를 활용해서
company, kind, region 으로 각 요소를 정보에 맞는 이름으로 변수를 만들어서 출력한다.
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:
anchors = post.find_all('a')
anchor = anchors[1]
link = anchor['href']
company, kind, region = anchor.find_all('span',class_='company')
print(company, kind, region)
출력값:
마지막으로 title 을 출력한다.
title 을 가져오기 위해 이번에는 find_all 메서드가 아닌 find 를 사용해본다.
find_all → list 를 준다
find → title class 가 있는 span 을 찾기 위해서 사용한다.
find_all 과 find 의 차이점은
find_all 은 리스트가 포함된 결과를 return 해주고
find 는 결과만 return 해준다.
beautifulsoup의 여러 메서드는 아래 사이트에서 확인할 수 있다.
https://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, 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
company, kind, region, job 의 title을 출력한다.
//////////////////////////// → 각 정보들을 구분하기 위해 사용
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:
anchors = post.find_all('a')
anchor = anchors[1]
link = anchor['href']
company, kind, region = anchor.find_all('span',class_='company')
title = anchor.find('span', class_='title')
print(company, kind, region, title)
print("////////////////////////")
print("////////////////////////")
출력값:
'Programming > Python 웹 스크래퍼 만들기' 카테고리의 다른 글
Python Beautifulsoup 복습 (0) | 2022.11.24 |
---|---|
Python Beautifulsoup를 이용한 Saving Results (0) | 2022.11.23 |
Python Job Posts (1) | 2022.11.21 |
Python Keyword Arguments (0) | 2022.11.18 |
Python BeautifulSoup (0) | 2022.11.17 |