-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
88 lines (67 loc) · 2.58 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
import os
import urllib.parse
from time import sleep
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
logging.getLogger().setLevel(logging.INFO)
BASE_URL = 'https://www.google.com/'
def get_title():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_experimental_option('prefs', {
'download.default_directory': os.getcwd(),
'download.prompt_for_download': False,
})
logging.info('Prepared chrome options..')
driver = webdriver.Chrome(chrome_options=chrome_options)
logging.info('Initialized chrome driver..')
with open('diykey.txt', 'r', encoding='utf-8') as f:
list_key = f.read().split('\n')
for key in list_key:
base_url = 'https://www.youtube.com/results?search_query='
url_search = base_url + urllib.parse.quote(key)
driver.get(url_search)
sleep(3)
html = driver.find_element_by_tag_name('html')
for i in range(2):
html.send_keys(Keys.END)
sleep(3)
ele_titles = driver.find_elements_by_css_selector(
'#video-title > yt-formatted-string')
titles = []
for e in ele_titles:
print(e.text)
titles.append(f'{key} - {e.text}')
with open('title_video', 'a+') as f:
f.write('\n'.join(titles))
driver.quit()
display.stop()
def firefox_example():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('driver.download.folderList', 2)
firefox_profile.set_preference('driver.download.manager.showWhenStarting', False)
firefox_profile.set_preference('driver.download.dir', os.getcwd())
firefox_profile.set_preference('driver.helperApps.neverAsk.saveToDisk', 'text/csv')
logging.info('Prepared firefox profile..')
driver = webdriver.Firefox(firefox_profile=firefox_profile)
logging.info('Initialized firefox driver..')
driver.get(BASE_URL)
logging.info('Accessed %s ..', BASE_URL)
logging.info('Page title: %s', driver.title)
driver.quit()
display.stop()
def main():
with open('diykey.txt', 'r', encoding='utf-8') as f:
list_key = f.read().split('\n')
for key in list_key:
get_title(key)
if __name__ == '__main__':
get_title()