[Python] How can I get data from booking.com to postgreSQL using presentation integration...

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    can anyone help me see the code to be able to get data into postgreDB, I am having problems getting data from booking.com to my side. This is the code of the integration method application. I am having trouble getting data from booking.com to postgreSQL. Hope everyone can help me, if possible, guide me more on the correct query syntax to create a table containing this data, that would be even better. Thank you.

    driver = webdriver.Chrome()
    driver.get("https://www.booking.com/")
    time.sleep(3)


    search_box = driver.find_element(By.NAME, "ss")
    search_box.send_keys("Da Nang")
    search_box.send_keys(Keys.RETURN)
    time.sleep(5)


    products = driver.find_elements(By.CSS_SELECTOR, "[data-testid='property-card']")

    try:
    conn = psycopg2.connect(host="localhost", user="postgres", password="22102003", database="shop")
    cursor = conn.cursor()
    print("Postgres connection successful!")

    for product in products[:7]: # Lấy 7 sản phẩm đầu tiên
    try:
    product_name = product.find_element(By.CSS_SELECTOR, "[data-testid='title']").text
    product_price = product.find_element(By.CSS_SELECTOR, "[data-testid='price-and-discounted-price']").text


    sql = "INSERT INTO products (name, price) VALUES (%s, %s)"

    cursor.execute(sql, (product_name, product_price))
    conn.commit()
    print(f"Đã lưu: {product_name} - {product_price}")

    except Exception as e:
    print("Error retrieving product data:", e)

    conn.close()
    except Exception as e:
    print("postgreSQL connection error:", e)

    driver.quit()

    Continue reading...

Compartilhe esta Página