Python靓汤意外串

2024-04-24 09:12:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用这个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

我使用的是python2.75。我完全不理解这个错误。为什么我会有这个错误?如何解决?提前谢谢你的帮助。一些论坛说,我应该给它第三个参数,但代码似乎逐字逐句对我来说,因为它是在大约9:45标记的视频。你知道吗


Tags: 代码fromimportrereadtitle错误tutorial