Python:在网页中查找字符串的一般方法--in

来源:互联网 发布:windows me u盘安装 编辑:程序博客网 时间:2024/05/21 11:55

如果只是要找出某一个或某些单词、字符串是否出现在某个网页中,只要使用in就可以了。


见下面例子:

import requestsurl = "https://www.baidu.com/s?wd=csdn%20%20abvedu&tn=95407960_s_hao_pg&ie=utf-8&ssl_sample=normal"html = requests.get(url).textprint(html)str = input("请输入要查找的字符串:")if str in html:    print("你找到了字符串:{}".format(str))else:    print("你要找的字符串没有出现在该网站!")

执行结果


<html><head><script>location.replace(location.href.replace("https://","http://"));</script></head><body><noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript></body></html>请输入要查找的字符串:baidu你找到了字符串:baiduProcess finished with exit code 0


0 0
原创粉丝点击