+ -
当前位置:首页 → 问答吧 → python 通过文本中的关键字匹配title的问题

python 通过文本中的关键字匹配title的问题

时间:2011-07-06

来源:互联网

Python code
url=["http://www.baidu.coml","http://www.google.com"]
f=open("1.txt").read()
lst=f.split()
for x in url:
   page=urllib2.urlopen(x)
    soup=BeautifulSoup(page,fromEncoding='gb2312')
    titletag=soup.title.string
   for y in lst:
        if y in  titletag:
                print x


报错: if x in soup.titletag.string:
TypeError: 'in <string>' requires string as left operand

我想根据记事本中列出的关键字,来和l列表中url标题进行对比,如果发现记事本中的关键字在url的标题中,就将该url打印出来。假设我记事本中的关键字为“百度”,那么按照我的想法,应该打印www.baidu.com。报错肯定是我代码的问题,我想知道怎么写,错在哪?

作者: xuzhijian17   发布时间: 2011-07-06

1. 贴完整错误
2. 学会阅读出错信息。看得懂TypeError: 'in <string>' requires string as left operand吗?

作者: iambic   发布时间: 2011-07-06