python method(requests, beautiful soup)를 이용해서 we work remotely라는 사이트에 있는 html 요소들을 스크랩 해보는 간단한 프로젝트
request : request는 해당 url에 요청을 보내고 응답을 받는다.
beautiful soup : beautiful soup는 해당 url에서 받은 html 코드 응답을 우리가 다룰 수 있는 python entity로 변환해준다.
from requests import get
from bs4 import BeautifulSoup
base_url = "<https://weworkremotely.com/remote-jobs/search?term=>"
search_term = 'react'
response = get(f"{base_url}{search_term}")
if response.status_code == 200:
results = []
soup = BeautifulSoup(response.text, "html.parser")
# response.text는 얻은 웹사이트의 코드를 준다.
jobs = soup.find_all('section', class_="jobs")
# beautiful soup가 python entity로 변환해주었기 때문에 job 내부를 검색할 수 있다.
for job in jobs:
job_posts = job.find_all('li') # section안의 li태그들을 찾아낸다
job_posts.pop(-1) # list의 마지막을 제거
for post in job_posts:
anchors = post.find_all('a') # li안의 a태그들을 찾아낸다
anchor = anchors[1]
# 첫번째 a태그는 우리가 원하는 정보를 포함하고 있지 않기 때문에 거르고 두번째 a태그를 가져온다.
# BeautifulSoup는 html태그들을 dictionary처럼 바꾸어준다.
link = anchor['href']
work = anchor.find_all('span', class_='company')
title = anchor.find('span', class_='title')
company,kind,region = work
# html 태그 제거
job_data = {
'link' : link,
'company' : company.string,
'kind' : kind.string,
'region' : region.string,
'title' : title.string
}
results.append(job_data)
for result in results:
print(result)
print('//////////////')
else:
print("Can not request Website")