Security Engineer 2022. 11. 18. 21:00

5.5 Keyword Arguments

 

이전에 beautifulsoup 를 사용해서 class_ = "jobs" 에 해당하는 웹사이트 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')
    jobs = print(soup.find_all('section',class_="jobs"))

여기서 jobs 변수에 넣은 class_="jobs" 는 keyword argument 라고 한다.

일반적으로 익숙한건 positional argument 이다.

 

사용자가 만든 funciton ( ) 소괄호안에 있는 공간의 이름을 parameter라고 한다.

실행할때 사용되는 function ( ) 소괄호 안에의 이름은 argument라고 한다.

아래 코드를 보면 positional argument 가 적용되어있다.

def say_hello(name,age): → parameter

say_hello("minsoo",20) → argument

positional argument 는 우리가 넣은 argument 값이 parameter 위치 순서에 따라 결과가 출력된다.

def say_hello(name, age):
    print(f"Hello {name} you are {age} years old")

say_hello("minsoo", 20)

출력값:

Hello minsoo you are 20 years old

 

 

argument 순서를 바꾸거나 parameter 순서를 바꾸면 결과값도 배치한 순서대로 바뀐다.

def say_hello(name, age):
    print(f"Hello {name} you are {age} years old")

say_hello(20, "minsoo")

출력값:

Hello 20 you are minsoo years old

 

 

keyword argument 는 positional argument 와 다르게 배치한 순서와 상관없이 argument 이름에 따라 출력된다.

argument 의 이름을 값과 매칭시켜주면 된다.

def say_hello(name, age):
    print(f"Hello {name} you are {age} years old")

say_hello(age=20, name="minsoo")

출력값:

Hello minsoo you are 20 years old

 

 

 

이전에 beautifulsoup 에서 find_all 메서드를 사용해서 class가 jobs인 section 들을 가져오는 코드를 작성했었다.

find_all 메서드에는 많은 argument 들이 있다. name, recursive, string, limit 등등

class_ 라고 하는 argument 를 사용하는 이유는 이미 class 라는 keyword가 python 에서 사용하고 있기때문에

class 를 사용하지 못하고 class_ 를 사용하였다.

if 나 else 를 변수명으로 설정할 수 없는 것과 같다.

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"))