2012-07-12 57 views
1

我对Python和BeautifulSoup4有一个有趣的问题。 我的方法是通过给定的餐厅(字典键)获取当地学生餐厅的菜单,然后显示这些菜单。BeautifulSoup在两种环境下的工作方式不同

def fetchFood(restaurant): 
    # Restaurant id's 
    restaurants = {'assari': 'restaurant_aghtdXJraW5hdHIaCxISX1Jlc3RhdXJhbnRNb2RlbFYzGMG4Agw', 'delica': 'restaurant_aghtdXJraW5hdHIaCxISX1Jlc3RhdXJhbnRNb2RlbFYzGPnPAgw', 'ict': 'restaurant_aghtdXJraW5hdHIaCxISX1Jlc3RhdXJhbnRNb2RlbFYzGPnMAww', 'mikro': 'restaurant_aghtdXJraW5hdHIaCxISX1Jlc3RhdXJhbnRNb2RlbFYzGOqBAgw', 'tottisalmi': 'restaurant_aghtdXJraW5hdHIaCxISX1Jlc3RhdXJhbnRNb2RlbFYzGMK7AQw'} 

if restaurants.has_key(restaurant.lower()): 
    soup = BeautifulSoup(urllib.urlopen("http://murkinat.appspot.com")) 
    meal_div = soupie.find(id="%s" % restaurants[restaurant.lower()]).find_all("td", "mealName hyphenate") 
    mealstring = "%s: " % restaurant 
    for meal in meal_div: 
    mealstring += "%s/" % meal.string.strip() 
    mealstring = "%s @ %s" % (mealstring[:-3], "http://murkinat.appspot.com") 
return mealstring 

else: 
    return "Restaurant not found" 

这将是我的IRCBot也的一部分,但目前它只能在我的测试机上(Ubuntu的12.04与Python 2.7.3),但在另一台机器上运行的机器人(Xubuntu上与Python 2.6.5),它失败。

soup = BeautifulSoup(urllib.urlopen("http://murkinat.appspot.com")) 

>>> type(soup) 
<class 'bs4.BeautifulSoup'> 

,我可以打印出来,它显示了具有所有这一切都应该是内容,但C和发现任何东西之后。如果我这样做:

>>> print soup.find(True) 
None 

>>> soup.get_text() 
u'?xml version="1.0" encoding="utf-8" ?' 

它停止读取到第一线,虽然其他计算机上,它完美地读取一切。

输出应该是这样的(从餐厅参数“Tottisalmi”工作机器在这个日期):

Tottisalmi: Sveitsinleike, kermaperunat/Jauheliha-perunamusaka/Uuniperuna, kylmäsavulohitäytettä/Kermainen herkkusienikastike @ http://murkinat.appspot.com 

我这个完全一无所知。我有很多类似于BeautifulSoup的解析方法,它们在bot上很好地工作(它解析url和维基百科的标题),但是这个解决方法让我烦恼不已。

有没有人有任何想法?我只能想出它与我的Python版本有关,这听起来很奇怪,因为在其他地方BeautifulSoup4可以正常工作。

回答

2

我相信你有different parsers installed on the two machines。 html5lib解析器在给定的标记上失败,从而导致不良行为。 lxml和html.parser解析器正确解析标记,并且不会给出不良行为。

在编写将在多台机器上运行的代码,这是最好的明确说明你想要的解析器使用方法:如果没有安装相应的解析器

BeautifulSoup(data, "lxml") 

这样你会得到一个错误。

+0

谢谢!我在某个时候遇到了这个问题,但我一定错过了它的意义。现在我得到了我的机器人并再次运行。 – Hamatti 2012-07-12 20:52:08