Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
嘿,我得到以下代码:
import urllib source = urllib.urlopen('WEBPAGE').read() if 'STRING TO LOOK FOR' in source: print 'MESSAGE' else: print 'else MESSAGE'
它检查特定字符串的网页源代码,有没有一种方法可以只让第一个让说20行代码被搜索到字符串?如果字符串不在那里,我会收到其他消息?
更改if 'STRING TO LOOK FOR' in source:为
if 'STRING TO LOOK FOR' in source:
if 'STRING TO LOOK FOR' in '\n'.join(source.split('\n', 20)[:20]):
将源代码分成几行,然后重新组合前 20 行。