Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用BeautifulSoup在html页面源代码中搜索特定关键字?_Python 3.x_Url_Beautifulsoup_Urllib - Fatal编程技术网

Python 3.x 如何使用BeautifulSoup在html页面源代码中搜索特定关键字?

Python 3.x 如何使用BeautifulSoup在html页面源代码中搜索特定关键字?,python-3.x,url,beautifulsoup,urllib,Python 3.x,Url,Beautifulsoup,Urllib,我的目标是找出如何在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.url

我的目标是找出如何在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)
但当我运行此命令时,它会返回一个空列表:

[]
有人能帮忙吗?提前感谢

试试:

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, 'html.parser')
print(keyword in soup.text)
印刷品:

True
True

或:

印刷品:

True
True

如果您的唯一目的是查看关键字是否存在,则不需要构造BeautifulSoup对象

from urllib import request

url_1 = "https://cheapchicsdesigns.com"
keyword ='cdn.secomapp.com'
page = request.urlopen(url_1)

print(keyword in page.read())
但是我建议您使用
请求
,因为它更简单

import requests

url_1 = "https://cheapchicsdesigns.com"
keyword ='cdn.secomapp.com'

res = requests.get(url_1)

print(keyword in res.text)

出于某种原因,它返回“False”。我使用了与上面提供的代码完全相同的代码。使用不同的关键字进行检查,但仍返回“False”。你知道这是什么原因吗?THX您运行的python版本是什么@猴面包树1988