2013-08-17 51 views
0

我希望你能告诉我我的webscraper出错的地方。如果声明不适用于刮刀

我想要做的是在页面上某个字符串(“Sorry,Gruen Fan”)发生变化时收到通知。我能够拉入字符串,但是,“If”函数似乎不起作用 - 它的输出应该是“Text in in”。下面的代码:

from bs4 import BeautifulSoup 
from urllib import urlopen 
import re 

urls= ["http://www.abc.net.au/tv/programs/gruen-nation/"] 

for url in urls: 
    webpage = urlopen(url).read() 
    FindTitle = re.compile('\t\t\t\t(.*)\.<BR><BR>') 
    FindTitle = re.findall(FindTitle,webpage) 
    print FindTitle[0] 
    print ' ' 

if 'Sorry, Gruen fan' in FindTitle: 
    print("Text is in") 
else: 
    print("Text isn't in") 

预先感谢您的时间,

萨姆。

回答

0

FindTitle是一个列表。该字符串不在列表中,因此您获得False

你应该检查它是否在字符串中的列表,而不是:

if 'Sorry, Gruen fan' in FindTitle[0]: 

而且,你不需要正则表达式,如果你只是要检查的字符串:

from urllib import urlopen 

urls = ["http://www.abc.net.au/tv/programs/gruen-nation/"] 

for url in urls: 
    html = urlopen(url).read() 

    if 'Sorry, Gruen fan' in html: 
     print("Text is in") 
    else: 
     print("Text isn't in") 
+0

辉煌,谢谢你的帮助。更简单=更好。 –