Reputation: 63
There is probably so many problems with this code that can't be answered in a single post. My main problem is I'm trying to lessen the amount of for loops with list comprehensions.
# import required libraries
import bs4, requests, re
# Get the HTML and store it in req variable
req = requests.get('Website')
# Make a BS object to help search HTML
reqSoup = bs4.BeautifulSoup(req.text, 'lxml')
# Search the ATIS under the 'pre' label and store it in reqSoupElems
reqSoupElems = reqSoup.select('pre')
# Create Regex's to search the ATIS
timeRegex = re.compile(r'\d\d\d\d\d\d')
WXRegex = re.compile(r'WX:\s.*')
# Create empty lists to store the data extracted for the ATIS
time = []
WX = []
# Create list with strings of time and weather elements extracted from ATIS
for i in range(len(reqSoupElems)):
# Create time list
time.append(timeRegex.search(str(reqSoupElems[i])).group())
# If the wx group isn't in ATIS append "Nill WX" to WX list
# Otherwise, append the weather in ATIS to WX list
if WXRegex.findall(str(reqSoupElems[i])) == []:
WX.append('Nil WX')
else:
WX.append(WXRegex.search(str(reqSoupElems[i])).group())
# Remove "WX: " from the strings within the lists
for i in range(len(WX)):
if WX[i][0:4] == 'WX: ':
WX[i] = WX[i][4:]
# Print to ensure it has worked
print('\n')
print(time)
print(len(time))
print('\n')
print(WX)
print(len(WX))
Upvotes: 1
Views: 404
Reputation: 140186
list comprehension isn't the only mprovement. Also get rid of for i in len(range(reqSoupElems))
antipattern by iterating directly on the elements:
time = [timeRegex.search(str(element)).group() for element in reqSoupElems]
Upvotes: 2