Friday, 2 November 2018

Can't find any idea to print data from mysql using function

I've created a script in python capable of collecting data from a webpage and storing the same into mysql. When the data are properly inserted into mysql, my script can, however, print them in the console.

My question is: how can I wrap the following three lines within a seperate function and print the data from the storage?

mycursor.execute("SELECT * FROM webdata")
for item in mycursor.fetchall():
    print(item)

My full script:

import mysql.connector
from bs4 import BeautifulSoup
import requests

URL = "https://www.tripadvisor.com.au/Restaurants-g255068-c8-Brisbane_Brisbane_Region_Queensland.html"

def get_info(link):
    mydb = mysql.connector.connect(
      host="localhost",
      user="root",
      passwd = "123",
      database="mydatabase"
    )
    mycursor = mydb.cursor()
    mycursor.execute("DROP TABLE if exists webdata")
    mycursor.execute("CREATE TABLE if not exists webdata (name VARCHAR(255), bubble VARCHAR(255), review VARCHAR(255))")

    response = requests.get(link)
    soup = BeautifulSoup(response.text,"lxml")
    for items in soup.find_all(class_="shortSellDetails"):
        name = items.find(class_="property_title").get_text(strip=True)
        bubble = items.find(class_="ui_bubble_rating").get("alt")
        review = items.find(class_="reviewCount").get_text(strip=True)

        mycursor.execute("INSERT INTO webdata (name,bubble,review) VALUES (%s,%s,%s)",(name,bubble,review))
        mydb.commit()

    #I wish to use the follwing three lines within another function to do the same

    mycursor.execute("SELECT * FROM webdata")
    for item in mycursor.fetchall():
        print(item)

if __name__ == '__main__':
    get_info(URL)



from Can't find any idea to print data from mysql using function

No comments:

Post a Comment