StackOverflow extract_job-2

StackOverflow extract_job-2

Python 공부 8일차

StackOverflow extract_jobs-1

# so.py
import requests
from bs4 import BeautifulSoup

URL = f"https://stackoverflow.com/jobs?q=python&sort=i"

def get_last_page():
  result = requests.get(URL)
  soup = BeautifulSoup(result.text, "html.parser")
  pages = soup.find("div", {"class" : "s-pagination"}).find_all("a")
  last_page = pages[-2].get_text(strip=True)
  return  int(last_page)

!!
def extract_job(html):
  title = html.find("div",{"class":"grid--cell fl1"}).find("h2").find("a")["title"]
  // h3안에 span이 2개가 있는 경우를 알았을 때 변수 선언
  company, location = html.find("div", {"class": "grid--cell fl1"}).find("h3").find_all("span", recursive=False)
  // recursive=False: 첫 요소만 가져올 수 있다.
  // get_text(): 텍스트만 가져오기(<span></span> 제거
  // strip=True: 공백 제거
  company = company.get_text(strip=True)
  // strip("-"): 텍스트에 있는 - 기호 제거
  // strip("\r"), strip("\n"): 공백제거(한 줄로 출력하기 위해)
  location = location.get_text(strip=True).strip("-").strip("\r").strip("\n")
  print(company, location)

  return {'title': title}
!!

def extract_jobs(last_page):
  jobs = []
  for page in  range(last_page):
    result = requests.get(f"{URL}&pg={page*1}")
    soup = BeautifulSoup(result.text, "html.parser")
    results = soup.find_all("div", {"class", "-job"})
    for result in results:
      job = extract_job(result)
      jobs.append(job)
  return jobs

def get_jobs():
  last_page = get_last_page()
  jobs = extract_jobs(last_page)
  return jobs

댓글

가장 많이 본 글