티스토리 뷰

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


교보문고 종합 주간 베스트 페이지에서 리스트를 추출하여 메타 정보로부터 필요한 정보를 추출합니다.


추출 정보 : title, author, image, url, originalPrice, salePrice



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
31
32
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):
# 첫번째 페이지에서 책의 상세 웹페이지 주소를 추출하여 리스트에 저장
    book_page_urls = []
    for cover in soup.find_all('div', {'class':'detail'}):
        link = cover.select('a')[0].get('href')
        book_page_urls.append(link)
 
    # 메타 정보로부터 필요한 정보를 추출
    for index, book_page_url in enumerate(book_page_urls):
        soup = get_soup(book_page_url)
        title = soup.find('meta', {'property':'rb:itemName'}).get('content')
        author = soup.select('span.name a')[0].text
        image = soup.find('meta', {'property':'rb:itemImage'}).get('content')
        url = soup.find('meta', {'property':'rb:itemUrl'}).get('content')
        originalPrice = soup.find('meta', {'property''rb:originalPrice'}).get('content')
        salePrice = soup.find('meta', {'property':'rb:salePrice'}).get('content')
        print(index+1, title, author, image, url, originalPrice, salePrice)    
 
 
target_url = 'http://www.kyobobook.co.kr/bestSellerNew/bestseller.laf'
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
공지사항
최근에 올라온 글
최근에 달린 댓글
링크