언어/[python]

[web_crawling] 웹 스크래퍼 만들기 1 - weworkremotely 사이트

dayeonsheep 2023. 5. 23. 02:56
from requests import get
from bs4 import BeautifulSoup

base_url = 'https://weworkremotely.com/remote-jobs/search?term='
search_term = "backend"

response = get(f"{base_url}{search_term}")
if response.status_code !=200: #200이어야 웹사이트에 접근 가능
    print("Can't request website")
else:
    results = []
    soup = BeautifulSoup(response.text, "html.parser") #response.text->웹사이트 코드 줌
    jobs = soup.find_all('section', class_="jobs") #class 이름이 jobs인 section 모두 찾기
    for job_section in jobs:
        job_posts= job_section.find_all('li') #section 내부의 li 모두 찾기
        job_posts.pop(-1) #그냥 버튼인 view-all이 마지막에 있어서 걔네 제거
        for post in job_posts:
            anchors = post.find_all('a') #<a ~ 
            anchor = anchors[1] #첫번째 건 tooltip이라고 안필요한거라 2번째 것만 얻기
            link = anchor["href"] #링크(href) 저장
            company, kind, region = anchor.find_all('span', class_='company') #anchor 내부에서 classrk company인 span 찾기
            title = anchor.find('span',class_='title') #title class인 span 하나! 찾기
            job_data = {
                'link': f'https://weworkremotely.com/{link}',
                'company' : company.string, #.string -> 태그 안에 있는 텍스트만 출력
                'region' : region.string,
                'title' : title.string
            }
            results.append(job_data) #for loop 안에 job_data가 있으니까 for문 밖에 list 만들어서 저장해주기
    for result in results:
        print(result)
        print('/'*10)

 

VSC에서 from requests랑 bs4가 이미 설치되어있는데도 오류가 났었는데

오른쪽 아래에 보이는 이 파이썬 버전을 바꿔주면 해결이 됐다