【问题标题】:How to locate a string while is running .read() on Python?在 Python 上运行 .read() 时如何定位字符串?
【发布时间】:2013-11-27 02:17:53
【问题描述】:

我正在使用 urllib2

try:
    msmalvo = urllib2.urlopen(website)
except URLError as e:
    msmalvo = e

if msmalvo.code == 200:
    if msmalvo.read() == '<head>':
        print 'Exits!'

    else:
        print 'Don't exist'

else:
    print ' '
    print msmalvo.code, ':/'

这个想法是确认在页面的源代码中找到了字符串“\head/”。 我该怎么做?

【问题讨论】:

    标签: python string locate


    【解决方案1】:

    简单地做:

    '<head>' in msmalvo.read()
    

    代替:

    msmalvo.read() == '<head>'
    

    【讨论】:

    • 我不明白'-'。你能解释一下吗?
    • 啊!我太笨了!哈哈哈。我是python的新手,我认为“in”只用于“for”语句xD!谢谢卡比亚 ;)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2016-05-15
    • 2015-06-25
    • 2021-01-29
    • 2021-10-21
    • 2018-02-06
    相关资源
    最近更新 更多