Мы делаем проект колледжа, где мы хотим извлечь данные из расписания колледжа и использовать его в нашем собственном проекте. У нас есть сценарий python, который извлекает данные, и он отлично работает на локальных машинах, однако, когда мы пытаемся использовать тот же скрипт на Amazon ec2, есть ошибка.Красивый сингл стола для настольного стола
from bs4 import BeautifulSoup
import requests
# url from timetable.ucc.ie showing 3rd Year semester 1 timetable
url = 'http://timetable.ucc.ie/showtimetable2.asp?filter=%28None%29&identifier=BSCS3&days=1-5&periods=1-20&weeks=5-16&objectclass=programme%2Bof%2Bstudy&style=individual'
# Retrieve the web page at url and convert the data into a soup object
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data)
# Retrieve the table containing the timetable from the soup object for parsing
timetable_to_parse = soup.find('table', {'class' : 'grid-border-args'})
i = 0 # i is an index into pre_format_day
pre_format_day = [[],[],[],[],[],[]] # holds un-formatted day information
day = [[],[],[],[],[],[]] # hold formatted day information
day[0] = pre_format_day[0]
# look at each td within the table
for slot in timetable_to_parse.findAll('td'):
# if slot content is a day of the week, move pointer to next day
# indicated all td's relating to a day have been looked at
if slot.get_text() in ('Mon', 'Tue' , 'Wed' , 'Thu' , 'Fri'):
i += 1
else: # otherwise the td related to a time slot in a day
try:
if slot['colspan'] is "4": #test if colspan of td is 4
# if it is, append to list twice to represent 2 hours
pre_format_day[i].append(slot.get_text().replace('\n',''))
pre_format_day[i].append(slot.get_text().replace('\n',''))
except:
pass
# if length of text of td is 1, > 11 or contains ":00"
if len(slot.get_text()) == 1 or len(slot.get_text()) > 11 or ":00" in\
slot.get_text():
# add to pre_format_day
pre_format_day[i].append(slot.get_text().replace('\n',''))
# go through each day in pre_format_day and insert formatted version in day[]
for i in range(1,6):
j = 0
while j < 20:
if len(pre_format_day[i][j]) > 10: # if there is an event store in day
day[i].append(pre_format_day[i][j])
else: # insert space holder into slots with no events
day[i].append('----- ')
j += 2
# creates a string containing a html table for output
timetable = '<table><tr>'
timetable += '<th></th>'
for i in range(0, 10):
timetable += '<th>' + day[0][i] + '</th> '
days = ['', 'Mon', 'Tue' , 'Wed' , 'Thu' , 'Fri']
for i in range(1,6):
timetable += '</tr><tr><th>' + days[i] + '</th>'
for j in range(0,10):
if len(day[i][j]) > 10:
timetable += '<td class="lecture">' + day[i][j] + '</td>'
else:
timetable += '<td></td>'
timetable += '</tr></table>'
# output timetable string
print timetable
Выход на локальном компьютере представляет собой таблицу, содержащую требуемые данные.
Выход на экземпляр ЕС2 является Traceback (самый последний вызов последнего): Файл "parse2.py", строка 21, в для слота в timetable_to_parse.findAll ('тд'): AttributeError: ' Объект NoneType 'не имеет атрибута' findAll '
Обе машины работают с Ubuntu 14.10, Python 2.7, но по какой-то причине я не могу понять, что, похоже, не получается получить требуемую страницу из URL-адреса и извлечь таблицу из этого но после этого я потерял.
Любая помощь очень ценится.
ли вы изучить HTML данные, которые вы получили назад при запуске на экземпляре ec2? Служит ли она другой странице? –
данные как и ожидалось –