-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (62 loc) · 2.31 KB
/
main.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
import os
import sys
import requests
from bs4 import BeautifulSoup
from flask import Flask, render_template, request
from lxml import etree
from urlextract import URLExtract
app = Flask(__name__)
serverId = 0
@app.route("/")
def hello_world():
print(os.getcwd() + '\\templates\\index.html')
return render_template('index.html', serverId=serverPort)
# perform extraction of the data by user choice of type
def extract_data(select_type, selector, soup):
data = []
if select_type == 'Tags':
data.append(soup.find_all(selector))
elif select_type == 'CSS':
data.append(soup.select(selector))
elif select_type == 'xPath':
tree = etree.HTML(str(soup))
element = tree.xpath(selector)
if len(element) > 0:
data.append(element)
elif select_type == 'Tag id':
data.append(soup.find(id=selector))
return data
def extract_urls(domain_name, soup):
extractor = URLExtract()
data = []
#gets all lines thats cmtains the domain to look for
for link in str(soup).splitlines():
if domain_name in link:
#check if the origin url exsits in list ebfore appending any further links to avoid recursive infinite scraping
if extractor.find_urls(link)[0] == domain_name:
print("same name as origin url")
else:
data.append(extractor.find_urls(link))
# print('-------------------------')
#data holds a list off all links that require additional serach-work
#data is to be sent to mngr for further distribution of work load
for itm in data:
print(itm)
@app.route("/start-crawl", methods=['POST'])
def start_crawl():
results = {}
url = request.form['url']
symbols = request.form['info']
select_type = request.form['dataType']
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
extract_urls(url, soup)
for tag_selector in symbols.splitlines():
results[tag_selector] = extract_data(select_type, tag_selector, soup)[0]
serverId = args = sys.argv[1:]
return render_template('results.html', name="crawling!!", url=url, results=results, serverId=serverPort)
if __name__ == '__main__':
args = sys.argv[1:]
serverPort = args[0]
print(serverPort)
app.run(debug=True, port=serverPort)