2013-10-31 42 views
0

我使用下面的代码从这个tutorialPython的再美丽的汤有意想不到的字符串

import urllib2 
from urllib import urlopen 
from BeautifulSoup import BeautifulSoup 
import re 

webpage = urlopen('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews').read 
# open webpage and read it web page is variable 

patFinderTitle = re.compile('<title>(.*)</title>') 
# get characters between titles 

patFinderLink = re.compile('<link rel.*href="(.*)" />') 

findPatTitle = re.findall(patFinderTitle, webpage) 
# variable is declared, uses re module to find all find two variables using the following args 
findPatLink = re.findall(patFinderLink, webpage) 

listIterator - [] 
listIterator[:] = range(2,16) 

soup2 = BeautifulSoup(webpage) 

print soup2.findAll('title') 

但是,我得到这个错误。

Traceback (most recent call last): 
    File "tutorial_re.py", line 14, in <module> 
    findPatTitle = re.findall(patFinderTitle, webpag 
    File "C:\Python27\lib\re.py", line 177, in findall 
    return _compile(pattern, flags).findall(string) 
TypeError: expected string or buffer 

我使用python 2.75。我根本不明白这个错误。为什么我有这个错误?我如何解决它?感谢您的帮助提前。有些论坛认为我应该给出第三个论点,但是代码似乎对我来说是逐字的,因为它是在9:45左右的视频中。

+1

不应该被它'的urlopen(' http://feeds.huffingtonpost.com/huffingtonpost/LatestNews').read()'? –

回答