-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplyScrape.py
150 lines (99 loc) · 3.67 KB
/
SimplyScrape.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from bs4 import BeautifulSoup, Comment
import lxml
from lxml import html
import os
import requests
import webbrowser
import time
from datetime import datetime
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.keys import Keys
from datetime import datetime
global chrome_path
chrome_path = "chromedriver.exe"
global driver
global soup
global scrapename
global scrapeurl
scrapename = input('The scrape name?')
scrapeurl = input('The scrape url?')
def scrapeit():
global driver
global soup
r = requests.get(scrapeurl)
soup = BeautifulSoup(r.content, 'lxml')
now = datetime.now()
date_time = now.strftime("%m-%d-%Y,%Hh%Mm%Ss")
filename = scrapename + '(' + date_time + ')' + ".html"
f = open(filename, "w", encoding='utf-8')
f.write(soup.prettify())
f.close()
print('results saved in: ' + filename)
print('Opening the html file..')
path = os.getcwd()
pathtofilefornotepad = path + '/' + filename
webbrowser.open(pathtofilefornotepad , new=2)
driver.get("file://" + pathtofilefornotepad)
driver.execute_script("window.stop();")
def useresults():
global soup
global scrapename
global scrapeurl
spanresults = []
divresults = []
atagresults = []
anyelementresults = []
#create the scrapecode text file
now = datetime.now()
date_time = now.strftime("%m-%d-%Y, %Hh%Mm%Ss")
filename = 'Scrapecode-' + scrapename + '(' + date_time + ')' + ".txt"
f = open(filename, "w", encoding='utf-8')
print('scrapecode will be saved in: ' + filename)
fs = open('sample-scrapecode.txt', mode='r', encoding='utf-8')
all_of_it = fs.read()
spaninput = input('Give class of a span element on the page:')
allspanresults = soup.find_all('span', class_ = spaninput)
for result in allspanresults:
spanresults.append(result.getText())
print(spanresults)
divinput = input('Give class of a div element on the page:')
alldivresults = soup.find_all('div', class_ = divinput)
for result in alldivresults:
divresults.append(result.getText())
print(divresults)
classinput = input('Give class of any element on the page:')
anyelementresults = soup.find_all("html_element", class_=classinput)
for result in anyelementresults:
anyelementresults.append(result.getText())
print(anyelementresults)
scrapeurlfile = 'scrapeurl = ' + " '" + scrapeurl + "' "
spaninputfile = 'spaninput = ' + " '" + spaninput + "' "
divinputfile = 'divinput = ' + " '" + divinput + "' "
classinputfile = 'classinput = ' + " '" + classinput + "' "
charcount = 0
for char in all_of_it:
charcount = charcount + 1
if charcount == 222:
f.write('\n')
f.write(scrapeurlfile + ' \n')
f.write(spaninputfile + '\n')
f.write(divinputfile + '\n')
f.write(classinputfile + '\n')
else:
f.write(char)
f.close()
fs.close()
print('scrapecode is saved.')
print('Opening scrapecode..')
path = os.getcwd()
pathtofilefornotepad = path + '/' + filename
webbrowser.open(pathtofilefornotepad , new=2)
def StartApp():
global driver
driver = webdriver.Chrome(chrome_path)
scrapeit()
useresults()
#program
StartApp()