Amazing-Python-Scripts
133 строки · 3.8 Кб
1import time2from selenium import webdriver3import sqlite34
5
6def sql_connection():7"""8Establishes a connection to the SQL file database
9:return connection object:
10"""
11con = sqlite3.connect('PlaystoreDatabase.db')12return con13
14
15def sql_table(con):16"""17Creates a table in the database (if it does not exist already)
18to store the app info
19:param con:
20:return:
21"""
22cur = con.cursor()23cur.execute("CREATE TABLE IF NOT EXISTS apps(QUERY text, URL text, NAME text, RATING text, "24" REVIEWS text, INSTALLS text, VERSION text, LASTUPDATE text, "25" COMPANY text, CONTACT text)")26con.commit()27
28
29def sql_insert_table(con, entities):30"""31Inserts the desired data into the table to store app info
32:param con:
33:param entities:
34:return:
35"""
36cur = con.cursor()37cur.execute('INSERT INTO apps(QUERY text, URL, NAME, RATING, REVIEWS, '38'INSTALLS, VERSION, LASTUPDATE, COMPANY, CONTACT) '39'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', entities)40con.commit()41
42
43driver = webdriver.Chrome()44
45con = sql_connection()46sql_table(con)47
48while 1:49query = input("\nEnter search query: ")50
51driver.get(f'https://play.google.com/store/search?q={query}&c=apps')52
53print('\nGetting all the desired info...\n')54time.sleep(5)55
56last_height = driver.execute_script("return document.body.scrollHeight")57time.sleep(5)58
59while True:60driver.execute_script(61"window.scrollTo(0, document.body.scrollHeight);")62
63time.sleep(5)64
65new_height = driver.execute_script("return document.body.scrollHeight")66if new_height == last_height:67break68last_height = new_height69
70store_urls = []71elems = driver.find_elements_by_xpath("//a[@href]")72for elem in elems:73if "details?id" in elem.get_attribute("href"):74store_urls.append((elem.get_attribute("href")))75
76store_urls = list(dict.fromkeys(store_urls))77
78for every in store_urls:79try:80driver.get(every)81url = every82time.sleep(3)83
84header1 = driver.find_element_by_tag_name("h1")85name = header1.text86
87star = driver.find_element_by_class_name("BHMmbe")88rating = star.text89
90comments = driver.find_element_by_class_name("EymY4b")91reviews = comments.text.split()[0]92
93stat_info_table = driver.find_elements_by_class_name("htlgb")94stats = []95for x in range(len(stat_info_table)):96if x % 2 == 0:97stats.append(stat_info_table[x].text)98
99stat_header = driver.find_elements_by_class_name("BgcNfc")100for x in range(len(stat_header)):101if stat_header[x].text == "Installs":102installs = stats[x]103
104if stat_header[x].text == "Current Version":105version = stats[x]106
107if stat_header[x].text == "Updated":108lastupdate = stats[x]109
110if stat_header[x].text == "Offered By":111company = stats[x]112
113if stat_header[x].text == "Developer":114for y in stats[x].split("\n"):115if "@" in y:116contact = y117break118
119entities = (query, url, name, rating, reviews, installs, version, lastupdate120version, lastupdate, company, email)121sql_insert_table(con, entities)122
123except Exception as e:124continue125
126print('\nAll info collected successfully!!\n')127
128ans = input('Press (y) to continue or any other key to exit: ').lower()129if ans == 'y':130continue131else:132print('Exiting..')133break134