
2023 - 02 - 03
weworkremotely에서 키워드에 따른 취업정보를 가져올 수 있는 프로젝트 실습을 진행했다.
from requests import get
from bs4 import BeautifulSoup
base_url = "https://weworkremotely.com/remote-jobs/search?term="
search_term = "java"
response = get(f"{base_url}{search_term}")
먼저, get과 BeautifulSoup 함수를 사용하기 위해 requests와 bs4를 import 해준다.
대상 사이트인 base_url과, 키워드인 search_term을 정의해 두고
f""로 합쳐준뒤, get으로 requests 상태를 받아온다.
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)) #len은 List나 Tuple의 크기를 줄여운다.
⦁ 지난 회차에 썼던 .status_code를 이용해 if문을 완성했다.
code가 200(정상)이면, BeautifulSoup(response.text, "html.parser")을 soup에 저장하는데, 이 코드의 의미는
"html 구조에 맞게 작성되어있는 문자열이니, html의 관점에 맞게 분석 해주세요" 라는 말과 동일하다.
⦁ soup.findall('section', class="jobs")는 jobs 클래스를 가진 section이 몇개 있는지 찾는다.
여기서 class_="jobs"는 keyword argument라고 하는데, 보통 우리에게 익숙한 것은 positional argument다.
def hello(name, age):
print(f"Hello {name} you are {age} years old")
이라고 했을 때, 두 가지 방법으로 함수를 호출해보자.
hello("James", 28) #positional
hello(age = 28, name = "James") #keyword
이 두가지는 모두 같은 동작을 하게 된다.
첫번째는 그 자리에 맞게 써주면 동작을 하게 되고
두번째는 그 자리에 맞게 써주지 않더라도 각 argument에 맞게 이름을 사용하면 결과가 동일하게 출력된다.
코드에 class_를 사용할수있는건 argument를 사용한것이다.
'__'를 쓰는 이유는 class라는 키워드는 이미 파이썬에서 사용하고 있기 때문이다.
⦁ 마지막으로 len(jobs)으로 확인된 jobs의 갯수를 확인한다.