2015-03-02 91 views
0

我们正在做一个大学项目,我们想从大学时间表中提取数据并将其用于我们自己的项目中。我们有一个提取数据的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 

本地机器上的输出是一个包含所需数据的表。

的EC2实例的输出是 回溯(最近通话最后一个):在timetable_to_parse.findAll 文件 “parse2.py”,第21行,在 的插槽( 'TD'): AttributeError的:' NoneType'对象没有属性'findAll'

这两台机器运行Ubuntu 14.10,Python 2.7,但由于某种原因,我不明白它似乎没有从URL中获取所需的页面,并从该表中提取表但之后,我输了。

任何帮助非常感谢。

+0

您是否检查过在ec2实例上运行时返回的html数据?它是否服务于不同的页面? – 2015-03-02 12:11:03

+0

数据如预期 – 2015-03-04 11:17:35

回答

2

问题是ec2在本地机器上使用了不同的解析器。用固定的 。

apt-get install python-lxml

0

登录到EC2实例并在Python CLI中逐行浏览它,直到找到问题。出于某种原因,BeautifulSoup解析在不同的系统上稍有不同。我有同样的问题,我不知道背后的原因。在不知道HTML内容的情况下,我们很难给你特定的帮助。

+0

因此,我尝试打印每个变量,他们打印并包括汤。如果我打印table_to_parse,我得到一个没有错误,汤的输出是http://cs1.ucc.ie/~jmt2/soup.txt – 2015-03-04 10:27:43