개발자/파이썬 Python

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

지구빵집 2022. 11. 22. 10:52
반응형

 

 

 

 

 

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

샘플 사이트로 자료를 얻어온 사이트 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으로 웹 스크래퍼 만들기 - 정신 없다.

 

 

반응형