728x90
반응형

Requests

import requests # requests 라이브러리 설치 필요

r = requests.get('http://spartacodingclub.shop/sparta_api/seoulair')
rjson = r.json()

rows = rjson['RealtimeCityAir']['row']

for row in rows:
    gu_name = row['MSRSTE_NM']
    gu_mise = row['IDEX_MVL']
    if gu_mise < 60:
        print(gu_name)

Requests + bs4 조합 기본코드

 

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

print (soup)

크롤링 예제1

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

#old_content > table > tbody > tr:nth-child(2) > td.title > div > a

movies = soup.select('#old_content > table > tbody > tr')

for moive in movies:
    a = moive.select_one('td.title > div > a')
    if a is not None:
        print(a.text)

크롤링 예제2

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

#old_content > table > tbody > tr:nth-child(2) > td.title > div > a

#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(3) > td:nth-child(1) > img

#old_content > table > tbody > tr:nth-child(2) > td.point
#old_content > table > tbody > tr:nth-child(9) > td.point

movies = soup.select('#old_content > table > tbody > tr')

for moive in movies:
    a = moive.select_one('td.title > div > a')

    if a is not None:
        title = a.text
        rank = moive.select_one('td:nth-child(1) > img')['alt']
        star = moive.select_one('td.point').text
        print(rank, title, star)
728x90
반응형

'코딩공부 > 파이썬' 카테고리의 다른 글

[3] 팬사이트(팬명록) 만들기 퀴즈  (0) 2022.10.26
[2] 영화 감상평 사이트  (0) 2022.10.26
[1] 화성땅 공동구매 프로젝트  (0) 2022.10.26
monggoDB 기초  (0) 2022.10.23
파이썬 함수  (0) 2022.10.23

+ Recent posts