W1-2 작업

2915 단어
from bs4 import BeautifulSoup

dic = {}
info = []

with open('./1_2_homework_required/index.html', 'r') as wb_data:
    soup = BeautifulSoup(wb_data, 'lxml')

    '''
    body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > img
    body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4:nth-child(2) > a
    body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4.pull-right
    body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > p
    body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p:nth-child(2) > span:nth-child(1)
    body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p.pull-right
    '''

    images = soup.select('body > div > div > div.col-md-9 > div > div > div > img')
    #print images
    #images1 = soup.select('body > div.container > div.row > div.col-md-9 > div.row > div.col-sm-4.col-lg-4.col-md-4 > div.thumbnail > img')
    #print images1
    titles = soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a')
    prices = soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right')
    #details = soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > p')
    score_counts = soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right')
    stars = soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)')

    #for image, title, price, detail, star, score_count in zip(images, titles, prices, details, stars, score_counts):
    for image, title, price, star, score_count in zip(images, titles, prices, stars, score_counts):
        dic = {
            'image':image.get('src'),
            'title':title.get_text(),
            'price':price.get_text(),
            #'detail':detail.get_text(),
            'star':len(star.find_all(class_='glyphicon glyphicon-star')),
            'score_count':score_count.get_text()
        }
        info.append(dic)

wb_data.close()

print info


총결산

  • css path에 비해 html 구조의 등급이 나에게 더 잘 어울린다
  • soup.select()에서 나오는 것은bs4입니다.element.Tag 유형, soup 객체 사용 방법
  • 좋은 웹페이지 즐겨찾기