【问题标题】:If Statement Not Working For Scraper如果语句不适用于 Scraper
【发布时间】:2013-08-17 07:37:32
【问题描述】:

我希望你能告诉我我的网络爬虫哪里出了问题。

我想做的是在页面上的某个字符串(“对不起,Gruen Fan”)发生更改时收到通知。我可以拉入字符串,但是,“If”函数似乎不起作用 - 它的输出应该是“Text is 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")

提前感谢您的宝贵时间,

山姆。

【问题讨论】:

    标签: if-statement beautifulsoup urllib urlopen


    【解决方案1】:

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 2021-12-02
      相关资源
      最近更新 更多