Esempio n. 1
0
def index():
    if request.method == "GET":
        sach_van_hoc = Book.objects(category="Sách văn học")
        sach_kinh_te = Book.objects(category="Sách kinh tế")
        manga_comics = Book.objects(category__icontains="Truyện tranh, Manga")
        sach_tong_hop = Book.objects(category="Sách kiến thức tổng hợp")
        sach_ngoai_ngu = Book.objects(category__icontains="ngoại ngữ")
        session['sachvanhoc'] = len(sach_van_hoc)
        session['sachkinhte'] = len(sach_kinh_te)
        session['mangacomics'] = len(manga_comics)
        session['sachtonghop'] = len(sach_tong_hop)
        session['sachngoaingu'] = len(sach_ngoai_ngu)
        return render_template('index.html')
    else:
        form = request.form
        title = form['title']
        title = convert(title)
        book_to_find = Book.objects(title_no_accent__icontains=title)
        return render_template("searchresult.html", book_to_find=book_to_find)
    # 2. Extract ROI (region of interest)
    soup = BeautifulSoup(html_content, "html.parser")

    a_list = soup.find_all('a', "product-item__thumbnail")

    for a in a_list:
        linkBuy_each = "https://www.adayroi.com" + a['href']
        linkBuy.append(linkBuy_each)
        img = a.img
        title.append(img['title']) 
        image.append(img['data-src'])

    price_list = soup.find_all("span", "product-item__info-price-sale")
    for p in price_list:
        price = p.string
        price = price[0:len(price)-1]
        price = price.replace(".","")
        price_sale.append(price)

for i in range(len(title)):
    new_book = Book(
        title = title[i],
        linkBuy = linkBuy[i],
        price_sale = price_sale[i],
        image = image[i],
        retailer = "Adayroi",
        category = "Sách ngoại ngữ",
        title_no_accent = convert(title[i])
        )
    new_book.save()