Python Re Beautiful Soup意外的字符串

时间:2013-10-31 17:45:41

标签: python beautifulsoup

我正在使用此tutorial中的以下代码。

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 个答案:

答案 0 :(得分:1)

实际调用read()时的输出是什么 - 带括号 - 在第6行?

webpage = urlopen('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews')。read()