본문 바로가기

개발자/파이썬 Python

파이선 웹 스크래퍼 만들어 보자. 코드 1

반응형

 

 

 

 

 

파이선 표준 라이브러리 문서 사이트

샘플 사이트로 자료를 얻어온 사이트 we work remotely

필요한 곳은 BeautifulSoup 패키지 설명 자료

서버에서 보내오는 HTTP 응답 상태 코드 , HTTP 상태 코드 정리한 다른 자료

파이선 라이브러리를 찾고 설명 자료 Python Package Index 

 

공부한 곳 마지막 링크인데 5.9 Recap 설명부터 본다. Python으로 웹 스크래퍼 만들기

 

 

from requests import get 

websites = (
  "google.com",
  "airbnb.com",
  "https://twitter.com",
  "facebook.com",
  "https://tiktok.com"
)

results = {}

for website in websites:
  if not website.startswith("https://"):
    website = f"https://{website}"
  response = get(website)
  if response.status_code == 200:
    results[website] = "OK"
  else:
    results[website] = "FAILED"
    
print(results)

 

결과는 한 줄로 나오는데 보기 쉽게 줄은 나누었다.

 

{
'https://google.com': 'OK', 
'https://airbnb.com': 'OK', 
'https://twitter.com': 'OK', 
'https://facebook.com': 'OK', 
'https://tiktok.com': 'OK'
}

 

실제로 사이트에 들어가 코드를 가져오기 시작하자.

 

from requests import get

base_url = "https://xxxxxxxxxx/remote-jobs/search?term="
search_term = "python"

response = get(f"{base_url}{search_term}")
if response.status_code != 200:
  print("Can't request website")
else
  print(response.text)

 

잘 나오는 마지막 코드는 

 

from requests import get
from bs4 import BeautifulSoup

base_url = "https://xxxxxxxxxxxxxxx/remote-jobs/search?term="
search_term = "java" #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 = soup.find_all('section', class_="jobs")
  results = []
  for job_section in jobs:
    job_posts = job_section.find_all('li')
    job_posts.pop(-1)
    for post in job_posts:
      anchor = post.find_all('a')
      anchor = anchor[1]
      link = anchor['href']
      company, kind, region = anchor.find_all('span', class_="company")
      title = anchor.find('span', class_='title')
      #print(company.string, kind.string, region.string, title.string) 
      job_data = {
        'compamy': company.string,
        'region': region.string,
        'position': title.string
      }
      results.append(job_data)

  for result in results:
    print(result)
    print("/////////")

 

결과 화면은 아래와 같다.

 

{'compamy': 'Simvoly', 'region': 'Anywhere in the World', 'position': 'Java Developer (Full-Stack)'}
/////////
{'compamy': 'Clevertech', 'region': 'Anywhere in the World', 'position': 'Senior Full Stack Engineer - Javascript'}
/////////
{'compamy': 'UpStack Technologies, Inc.', 'region': 'Latin America Only/Europe Only', 'position': 'Full stack Javascript Developer (React.js + Node.js)'}
/////////
{'compamy': 'SocialHub', 'region': 'Anywhere in the World', 'position': 'Backend Javascript / Node.js Developer - Remote/SaaS (m/f/d)'}
/////////
{'compamy': 'Vetted Biz', 'region': 'Latin America Only', 'position': 'Senior Full Stack Engineer (PHP/CSS/HTML/JavaScript)'}
/////////
{'compamy': "Jack's Flight Club", 'region': 'Europe Only', 'position': "Python & JavaScript Full Stack Developer at Jack's Flight Club"}
/////////
{'compamy': 'Phase Locked Software', 'region': 'Anywhere in the World', 'position': 'Fullstack JavaScript Developer (Remote / Part-time / Freelance)'}
/////////
{'compamy': 'Toggl', 'region': 'Europe Only', 'position': 'Remote Frontend Developer (JavaScript + React)'}
/////////
{'compamy': 'Clevertech', 'region': 'Anywhere in the World', 'position': 'Senior Frontend Developer - Javascript React'}
/////////
{'compamy': 'Clevertech', 'region': 'Anywhere in the World', 'position': 'Senior Backend Engineer - Javascript Node'}
/////////
{'compamy': 'Files.com', 'region': 'USA Only', 'position': 'FTP and SFTP Server Developer (Java)'}
/////////
{'compamy': 'Data Virtuality GmbH', 'region': 'Anywhere in the World', 'position': 'Senior Backend Developer (Java)'}
/////////
{'compamy': 'Kinsta', 'region': 'Europe Only/Africa Only', 'position': 'Senior JavaScript Developer'}
/////////
{'compamy': 'Vidalytics', 'region': 'Europe Only', 'position': 'Lead / Senior Java QA Engineer'}
/////////
{'compamy': 'DebugBear', 'region': 'Anywhere in the World', 'position': 'Full Stack JavaScript Developer'}
/////////
{'compamy': 'Nagarro Digital Ventures', 'region': 'Anywhere in the World', 'position': 'Senior Java Engineer'}
/////////

 

 

여기까지 하고 다음 단계로 넘어간다. 또 듣고 쓴다.

 

 

Python으로 웹 스크래퍼 만들기 - 정신 없다.

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

귀사가 성공하기까지의 긴 고난의 시간을 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩