Code Monkey home page Code Monkey logo

tiki_scraping_cuong_felix's Introduction

Background

Project Tiki Scraping (Flask App & POSTGRE)


Launch the app

# launch the application
python app.py

Data architecture

Products Type
id integer
title character varying(255)
image text
category_id integer
created_at timestamp without time zone
product_url text
final_price character varying(255)
Categories Type
id integer
name character varying(255)
url text
parent_id integer
created_at timestamp without time zone
  • Connect: We use psycopg2 LB to connect the python with PSQL. Change [User] and [Database] to yours to use this code
  • Data collected: product title, brand name, old price, discount, final price, category, ratings, TikiNOW availability, image Url
  • Take alook at [PhaCu_craw_code.ipynb] to see how to crawl all products from <tiki.vn>. We overcome the duplication of Product data by using the Categories Tree. In particular, we only get Product’s data from the deepest SubCategories, so the data is not duplicated.

Query to find the deepest category

query = """
        SELECT cp.parent_id, cp.name, cp.url, id as child_id FROM cat_parents as cp
         LEFT JOIN categories as c on cp.parent_id = c.parent_id
         WHERE id is null
        ; """
cur.execute(query)
cat=cur.fetchall()

Example of scraping

def parse(url):
    try:
        response = requests.get(url).text
        response = BeautifulSoup(response, "html.parser")
        return response
    except Exception as err:
        print(f'ERROR: {err}')
        return ''


def crawl_sub_categories(category, save_db=False):
    name = category.name
    url = category.url
    sub_categories = []

    try:
        div_containers = parse(url).find_all(
            'div', attrs={"class": "list-group-item is-child"})
        for div in div_containers:
            sub_id = None
            sub_name = div.a.text
            sub_url = 'https://tiki.vn' + div.a.get('href')
            sub_parent_id = category.cat_id

            cat = Category(sub_id, sub_name, sub_url, sub_parent_id)
            if save_db:
                cat.save_into_db()
            if cat.cat_id is not None:
                sub_categories.append(cat)
    except Exception as err:
        print(f'ERROR: {err}')

    return sub_categories

Search method used

# from app.py
@app.route('/', methods=['GET', 'POST'])
def index():
    form = SearchForm()
    if form.validate_on_submit():
        productsToShow = products.get_search_products(form.search.data)
    else:
        productsToShow = products.get_products()

# from product.py
def get_search_products(search):
    cursor.execute("SELECT * FROM products WHERE title LIKE  '%" +
                   str(search) + "%' LIMIT 9")
    products = cursor.fetchall()
    return products

Contact:

Cuong & Félix

Background

tiki_scraping_cuong_felix's People

Contributors

felixbecquart1990 avatar phacu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.