Amazing-Python-Scripts

Форк
0
133 строки · 3.8 Кб
1
import time
2
from selenium import webdriver
3
import sqlite3
4

5

6
def sql_connection():
7
    """
8
    Establishes a connection to the SQL file database
9
    :return connection object:
10
    """
11
    con = sqlite3.connect('PlaystoreDatabase.db')
12
    return con
13

14

15
def sql_table(con):
16
    """
17
    Creates a table in the database (if it does not exist already)
18
    to store the app info
19
    :param con:
20
    :return:
21
    """
22
    cur = con.cursor()
23
    cur.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)")
26
    con.commit()
27

28

29
def sql_insert_table(con, entities):
30
    """
31
    Inserts the desired data into the table to store app info
32
    :param con:
33
    :param entities:
34
    :return:
35
    """
36
    cur = con.cursor()
37
    cur.execute('INSERT INTO apps(QUERY text, URL, NAME, RATING, REVIEWS, '
38
                'INSTALLS, VERSION, LASTUPDATE, COMPANY, CONTACT) '
39
                'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', entities)
40
    con.commit()
41

42

43
driver = webdriver.Chrome()
44

45
con = sql_connection()
46
sql_table(con)
47

48
while 1:
49
    query = input("\nEnter search query: ")
50

51
    driver.get(f'https://play.google.com/store/search?q={query}&c=apps')
52

53
    print('\nGetting all the desired info...\n')
54
    time.sleep(5)
55

56
    last_height = driver.execute_script("return document.body.scrollHeight")
57
    time.sleep(5)
58

59
    while True:
60
        driver.execute_script(
61
            "window.scrollTo(0, document.body.scrollHeight);")
62

63
        time.sleep(5)
64

65
        new_height = driver.execute_script("return document.body.scrollHeight")
66
        if new_height == last_height:
67
            break
68
        last_height = new_height
69

70
    store_urls = []
71
    elems = driver.find_elements_by_xpath("//a[@href]")
72
    for elem in elems:
73
        if "details?id" in elem.get_attribute("href"):
74
            store_urls.append((elem.get_attribute("href")))
75

76
    store_urls = list(dict.fromkeys(store_urls))
77

78
    for every in store_urls:
79
        try:
80
            driver.get(every)
81
            url = every
82
            time.sleep(3)
83

84
            header1 = driver.find_element_by_tag_name("h1")
85
            name = header1.text
86

87
            star = driver.find_element_by_class_name("BHMmbe")
88
            rating = star.text
89

90
            comments = driver.find_element_by_class_name("EymY4b")
91
            reviews = comments.text.split()[0]
92

93
            stat_info_table = driver.find_elements_by_class_name("htlgb")
94
            stats = []
95
            for x in range(len(stat_info_table)):
96
                if x % 2 == 0:
97
                    stats.append(stat_info_table[x].text)
98

99
            stat_header = driver.find_elements_by_class_name("BgcNfc")
100
            for x in range(len(stat_header)):
101
                if stat_header[x].text == "Installs":
102
                    installs = stats[x]
103

104
                if stat_header[x].text == "Current Version":
105
                    version = stats[x]
106

107
                if stat_header[x].text == "Updated":
108
                    lastupdate = stats[x]
109

110
                if stat_header[x].text == "Offered By":
111
                    company = stats[x]
112

113
                if stat_header[x].text == "Developer":
114
                    for y in stats[x].split("\n"):
115
                        if "@" in y:
116
                            contact = y
117
                            break
118

119
                entities = (query, url, name, rating, reviews, installs, version, lastupdate
120
                            version, lastupdate, company, email)
121
                sql_insert_table(con, entities)
122

123
        except Exception as e:
124
            continue
125

126
    print('\nAll info collected successfully!!\n')
127

128
    ans = input('Press (y) to continue or any other key to exit: ').lower()
129
       if ans == 'y':
130
            continue
131
        else:
132
            print('Exiting..')
133
            break
134

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.