Skip to content

Commit b97456b

Browse files
committed
new examples
1 parent 950b220 commit b97456b

File tree

4 files changed

+182
-0
lines changed
  • __scraping__/maharerait.mahaonline.gov.in - selenium - download PDF from object
  • tkinter

4 files changed

+182
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
After clicking it adds <object data="application/pdf;base64,..."> which has all PDF as text encoded bas64 in data=
2+
3+
driver.execute_script("arguments[0].click();",btn)
4+
5+
time.sleep(5)
6+
7+
# get tag <object>
8+
obj = driver.find_element_by_tag_name('object')
9+
10+
# get `data=`
11+
data = obj.get_attribute('data')
12+
13+
# get text after `base64,`
14+
text = data.split(',')[1]
15+
16+
# encode text to PDF's content (as bytes)
17+
import base64
18+
bytes = base64.b64decode(text)
19+
20+
# save bytes in file
21+
with open('output.pdf', 'wb') as fp:
22+
fp.write(bytes)
23+
24+
And now you have all in output.pdf
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
# date: 2019.07.18
3+
# https://stackoverflow.com/questions/57088227/save-the-pdf-using-the-selenium-webdriver-in-python/57089444#57089444
4+
5+
from selenium import webdriver
6+
from selenium.webdriver.common.by import By
7+
from selenium.webdriver.support.ui import WebDriverWait
8+
from selenium.webdriver.support import expected_conditions as EC
9+
from selenium.webdriver.common.keys import Keys
10+
import urllib.request
11+
from bs4 import BeautifulSoup
12+
import os
13+
from selenium.webdriver.support.select import Select
14+
import time
15+
16+
url = 'https://maharerait.mahaonline.gov.in'
17+
#chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'
18+
19+
driver = webdriver.Firefox()#executable_path=chrome_path)
20+
driver.get(url)
21+
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='search-pro-details']//a[contains(.,'Search Project Details')]"))).click()
22+
Registered_Project_radio = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"Promoter")))
23+
24+
driver.execute_script("arguments[0].click();",Registered_Project_radio)
25+
26+
Application = driver.find_element_by_id("CertiNo")
27+
Application.send_keys("P50500000005")
28+
29+
Search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSearch")))
30+
driver.execute_script("arguments[0].click();", Search)
31+
View = [item.get_attribute('href') for item in driver.find_elements_by_tag_name("a") if item.get_attribute('href') is not None]
32+
33+
btn = WebDriverWait(driver,
34+
20).until(EC.element_to_be_clickable((By.XPATH,
35+
"//a[@class='btn btn-md btn-success' and @id='btnShow_2017']")))
36+
37+
driver.execute_script("arguments[0].click();",btn)
38+
39+
time.sleep(5)
40+
41+
obj = driver.find_element_by_tag_name('object')
42+
data = obj.get_attribute('data')
43+
text = data.split(',')[1]
44+
45+
import base64
46+
bytes = base64.b64decode(text)
47+
48+
with open('output.pdf', 'wb') as fp:
49+
fp.write(bytes)

tkinter/animated-label/main.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
# date: 2019.07.18
3+
4+
import tkinter as tk
5+
from PIL import Image, ImageTk
6+
7+
class AnimatedLabel(tk.Label):
8+
9+
def __init__(self, *args, images=None, delay=100, state='Idle', **kwargs):
10+
super().__init__(*args, **kwargs)
11+
12+
self.images = images
13+
self.delay = delay
14+
self.state = state
15+
self.current_frame = 0
16+
17+
def start(self):
18+
self.next_frame()
19+
20+
def next_frame(self):
21+
if self.state == 'Idle':
22+
# change image
23+
self.frame = self.images[self.current_frame]
24+
self.configure(image=self.frame)
25+
26+
# get next frame (or first frame)
27+
self.current_frame = (self.current_frame+1) % len(self.images)
28+
29+
# run again after 'delay' milliseconds
30+
self.after(self.delay, self.next_frame)
31+
32+
class Application():
33+
34+
def __init__(self):
35+
36+
# WINDOW SETUP
37+
self.root = tk.Tk()
38+
self.root.geometry('512x512')
39+
self.root.protocol('WM_DELETE_WINDOW', self.annihilation)
40+
41+
self.load_images()
42+
43+
# CALL THE ANIMATION FUNCTION
44+
self.animation1 = AnimatedLabel(self.root, images=self.images, delay=500)
45+
self.animation1.place(relx=.5, rely=.5, anchor="c")
46+
47+
48+
self.animation2 = AnimatedLabel(self.root, images=self.images, delay=125)
49+
self.animation2.place(relx=.1, rely=.1, anchor="c")
50+
51+
self.animation1.start()
52+
self.animation2.start()
53+
54+
self.root.mainloop()
55+
56+
57+
# LIST OF IMAGES
58+
def load_images(self):
59+
self.filenames = [
60+
'Obrazy/21586784_1642005829205281_8345912444787042013_o.jpg',
61+
'Obrazy/37884728_1975437959135477_1313839270464585728_n.jpg'
62+
#'Image1.tif',
63+
#'Image2.tif',
64+
]
65+
self.images = []
66+
for filename in self.filenames:
67+
img = ImageTk.PhotoImage(Image.open(filename).resize((512, 512)))
68+
self.images.append(img)
69+
70+
def annihilation(self):
71+
#self.root.eval('::ttk::CancelRepeat')
72+
self.state = 'Quitting'
73+
self.root.destroy()
74+
75+
Application()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
# 2019.07.18
3+
4+
import tkinter as tk
5+
6+
def set_bold():
7+
try:
8+
textbox.tag_add('bold', 'sel.first', 'sel.last')
9+
except Exception as ex:
10+
# text not selected
11+
print(ex)
12+
13+
def set_red():
14+
try:
15+
textbox.tag_add('red', 'sel.first', 'sel.last')
16+
except Exception as ex:
17+
# text not selected
18+
print(ex)
19+
20+
root = tk.Tk()
21+
22+
textbox = tk.Text(root)
23+
textbox.pack()
24+
25+
textbox.tag_config('bold', font=('Arial', 10, 'bold'))
26+
textbox.tag_config('red', foreground='red')
27+
28+
button1 = tk.Button(root, text='Bold', command=set_bold)
29+
button1.pack()
30+
31+
button2 = tk.Button(root, text='Red', command=set_red)
32+
button2.pack()
33+
34+
root.mainloop()

0 commit comments

Comments
 (0)