python-3.x - 如何使用 BeautifulSoup 在 html 页面源代码中搜索特定关键字?
问题描述
我的目标是找出如何在 html 页面源代码中搜索特定关键字并将值返回为 True/False。取决于是否找到了关键字。
我正在寻找的特定关键字是“cdn.secomapp.com”
现在我的代码如下所示:
from urllib import request
from bs4 import BeautifulSoup
url_1 = "https://cheapchicsdesigns.com"
keyword ='cdn.secomapp.com'
page = request.urlopen(url_1)
soup = BeautifulSoup(page)
soup.find_all("head", string=keyword)
但是当我运行它时,它会返回一个空列表:
[]
有人可以帮忙吗?提前致谢
解决方案
如果您的唯一目的是查看关键字是否存在,那么您不需要构造 BeautifulSoup 对象。
from urllib import request
url_1 = "https://cheapchicsdesigns.com"
keyword ='cdn.secomapp.com'
page = request.urlopen(url_1)
print(keyword in page.read())
但我建议您使用它,requests
因为它更容易
import requests
url_1 = "https://cheapchicsdesigns.com"
keyword ='cdn.secomapp.com'
res = requests.get(url_1)
print(keyword in res.text)
推荐阅读
- r - 仅缩短标签 fe 的首字母
- json - 如何使用 RxJS 进行 groupBy 并在特定数组中获取结果?
- python - 当找到 list2 中的项目时,移动到 list1 中的下一个项目,同时比较两个列表
- python - 替换字典列表中的键的有效方法
- ios - 无法更新领域对象
- go - 如何使用 golang 为 lambda 函数提供配置值
- wolkenkit - 没有 Docker 的 wolkenkit
- android - 是否可以将 LAN 连接到 Android 模拟器
- typescript - 如何创建一个可能只包含特定类和该类的子类的字典变量
- xamarin.forms - 在 21 之前的版本中,什么相当于 Android.Widget.ListView.NestedScrollingEnabled?