본문 바로가기
Python/Web Scrapping

<class 'bs4.element.ㅁㅁㅁ'>

by Sondho 2020. 7. 18.

type() 결과

  • .select()
    <class 'bs4.element.ResultSet'>
  • for i in select(): 일 때, i
    <class 'bs4.element.Tag'>
  • .find()
    <class 'bs4.element.Tag>
  • .find_all()
    <class 'bs4.element.ResultSet'>

 

<class 'bs4.element.ResultSet'>

list처럼 사용 가능 하다.

  - 슬라이싱 가능. items[start : end : step]

  - 인덱싱 가능. items[0]

 

import requests
from bs4 import BeautifulSoup

url = "https://www.iban.com/currency-codes"

request = requests.get(url)
soup = BeautifulSoup(request.text, "html.parser")

table = soup.find("table")
rows = table.find_all("tr")[1:]
print(rows[0])
print(type(rows))

#####
# 결과 :
# <tr>
# <th class="head">Country</th>
# <th class="head">Currency</th>
# <th class="head">Code</th>
# <th class="head">Number</th>
# </tr>
# <class 'bs4.element.ResultSet'>

 

import requests
from bs4 import BeautifulSoup

url = "https://www.iban.com/currency-codes"
request = requests.get(url)
soup = BeautifulSoup(request.text, "html.parser")

table = soup.find("table")
rows = table.find_all("tr")[1:]

for row in rows:
    items = row.find_all("td")
    print(items)
    print(type(items))
    
    
#####
# 결과
# ...
# <class 'bs4.element.ResultSet'>
# [<td>AFGHANISTAN</td>, <td>Afghani</td>, <td>AFN</td>, <td>971</td>]
# ...

 

 

<class 'bs4.elemet.Tag'>

import requests
from bs4 import BeautifulSoup


url = "https://www.iban.com/currency-codes"
soup = BeautifulSoup(requests.get(url).text, "html.parser")
tr = soup.select("tbody > tr")

def get_data():
    for i in tr:
        print(f"type(tr): {type(tr)}")
        print(f"i:\n{i}")
        print(f"type(i): {type(i)}")
        td_find = i.find("td")
        print(f"td_find: {td_find}")
        print(f"type(td_find): {type(td_find)}")
        td_find_all = i.find_all("td")
        print(f"td_find_all: {td_find_all}")
        print(f"type(td_find_all): {type(td_find_all)}")
        print("=" * 50)
        
get_data()

#####
결과: 
......
==================================================
type(tr): <class 'bs4.element.ResultSet'>
i:
<tr>
<td>ZIMBABWE</td>
<td>Zimbabwe Dollar</td>
<td>ZWL</td>
<td>932</td>
</tr>
type(i): <class 'bs4.element.Tag'>
td_find: <td>ZIMBABWE</td>
type(td_find): <class 'bs4.element.Tag'>
td_find_all: [<td>ZIMBABWE</td>, <td>Zimbabwe Dollar</td>, <td>ZWL</td>, <td>932</td>]
type(td_find_all): <class 'bs4.element.ResultSet'>
==================================================

'Python > Web Scrapping' 카테고리의 다른 글

.find_all("tr")과 .find_all("tr")[:]  (0) 2020.07.18
string, strip()을 이용한 문자 추출과 정리  (0) 2020.04.02
1. requests와 beautifulsoup  (0) 2020.04.01

댓글