-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscraping.py
142 lines (114 loc) · 4.28 KB
/
scraping.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
# Python BEM - Blade Element Momentum Theory Software.
# Copyright (C) 2022 M. Smrekar
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from urllib.parse import parse_qsl, urlparse
import numpy as np
import requests
from bs4 import BeautifulSoup as bs
def scrape_data(link):
"""
:param link:
:return:
"""
out = []
try:
data = get_polars(link)
except Exception as e:
print(e)
return None
for Re, value in data.items():
for ncrit, value2 in value.items():
for alpha, value3 in value2.items():
cl = value3["cl"]
cd = value3["cd"]
out.append([Re, ncrit, alpha, cl, cd])
out = np.array(out)
return out
def get_polars(link):
"""
:param link:
:return:
"""
results = {}
print("Getting data from", link)
r = requests.get(link)
txt = r.text
soup = bs(txt, features='html.parser')
table_of_polars = soup.find_all("table", {"class": "polar"})[0]
rows = table_of_polars.find_all("tr")[1:-1]
links = []
for r in rows:
data = r.find_all("td")
checkbox, name, reynolds, ncrit, maxclcd, description, source, details = [
d for d in data]
details_link = "http://airfoiltools.com" + \
details.find_all("a")[0].get('href')
links.append(details_link)
print("List of links:", links)
csv_links = []
for l in links:
print("getting csv links from", l)
r = requests.get(l)
txt = r.text
soup = bs(txt, features='html.parser')
details_table = soup.find_all("table", {"class": "details"})[0]
td1 = details_table.find_all("td", {"class": "cell1"})
_links = details_table.find_all("a")
for _l in _links:
if "csv?polar" in _l.get("href"):
csv_links.append("http://airfoiltools.com" + _l.get("href"))
break
print("CSV links:", csv_links)
for l in csv_links:
print("getting data from csv link", l)
lines = None
r = requests.get(l)
text = r.text
lines = text.splitlines()
start = lines.index("Alpha,Cl,Cd,Cdp,Cm,Top_Xtr,Bot_Xtr")
reynolds_number = [float(i.split(",")[1]) for i in lines if "Reynolds number," in i][0]
ncrit = [float(i.split(",")[1]) for i in lines if "Ncrit," in i][0]
mach = [float(i.split(",")[1]) for i in lines if "Mach," in i][0]
for line_num in range(start + 1, len(lines)):
alpha, cl, cd = None, None, None
alpha, cl, cd = lines[line_num].split(",")[:3]
alpha, cl, cd = float(alpha), float(cl), float(cd)
if not reynolds_number in results.keys():
results[reynolds_number] = {}
if not ncrit in results[reynolds_number].keys():
results[reynolds_number][ncrit] = {}
results[reynolds_number][ncrit][alpha] = {"cl": cl, "cd": cd}
return results
def get_x_y_from_link(link):
"""
:param link:
:return:
"""
print("Getting x-y airfoil data from", link)
params = parse_qsl(urlparse(link.strip()).query, keep_blank_values=True)
selig_link = "http://airfoiltools.com/airfoil/seligdatfile?airfoil=" + params[0][1]
print("Presumed Selig dat link:")
print(selig_link)
r = requests.get(selig_link)
text = r.text
print("Got text:")
print(text)
print("Parsing...")
lines = text.splitlines()
x, y = [], []
for l in lines[1:]:
stripped_line = l.strip().replace(" ", " ")
x_l, y_l = stripped_line.split(" ")
x.append(float(x_l))
y.append(float(y_l))
return x, y
# scrape_data("http://airfoiltools.com/airfoil/details?airfoil=clarky-il")