0

我正在使用本教程中的以下代码。

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 左右的视频中。

4

1 回答 1

1

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

网页 = urlopen(' http://feeds.huffingtonpost.com/huffingtonpost/LatestNews ').read()

于 2013-10-31T17:51:52.710 回答