티스토리 뷰

파이썬 웹 크롤링 연습 예제 #1


특정 사이트의 웹 크롤링을 위해서 파이썬의 함수중에 Beautifulsoup을 이용하여 table 안에 있는 내용을 추출하는 파이썬 크롤링 연습한 예제 내용입니다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from bs4 import BeautifulSoup
import urllib.request
 
def get_soup(target_url):
    html = urllib.request.urlopen(target_url).read()
    soup = BeautifulSoup(html, 'html.parser')
    return soup
 
 
def extract_data(soup):
    table = soup.find('table', {'class''grid'})
    trs = table.find_all('tr')
    for idx, tr in enumerate(trs):
        if idx > 0:
            tds = tr.find_all('td')
            sequence = tds[0].text.strip()
            description = tds[1].text.strip()
            solved_num = tds[2].text.strip()
            print(sequence, description, solved_num)
 
 
for i in range(17):
    target_url = 'http://euler.synap.co.kr/prob_list.php?pg={}'.format(i)
    soup = get_soup(target_url)
    extract_data(soup)
cs





댓글
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
공지사항
최근에 올라온 글
최근에 달린 댓글
링크