Search code examples
python-3.xurlbeautifulsoupurllib

How to search for a specific keyword in html page source code with BeautifulSoup?


I'm aiming to find out how can I search for a specific keyword in the html page source code and return value as True/False. Depending if the keyword has been found or not.

Specific keyword I'm looking for is 'cdn.secomapp.com'

For now my code looks like this:

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)

but when I run this it then returns an empty list:

[]

Could someone help with this? thanks in advance


Solution

  • If your only purpose is to see whether the keyword is present or not, then you don't need to construct a BeautifulSoup object.

    from urllib import request
    
    url_1 = "https://cheapchicsdesigns.com"
    keyword ='cdn.secomapp.com'
    page = request.urlopen(url_1)
    
    print(keyword in page.read())
    

    But I would recommend you to use requests as it's more easy

    import requests
    
    url_1 = "https://cheapchicsdesigns.com"
    keyword ='cdn.secomapp.com'
    
    res = requests.get(url_1)
    
    print(keyword in res.text)