The code is working if it's just one word. However, I want to find a series of words. If any of those words (ex. ["Mobile", "standalone", "desktop"]) pops up, it should output that found word. Any ideas?
from selenium import webdriver
from time import sleep
from datetime import datetime
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(options=options)
browser.get('https://sites.google.com/chromium.org/driver/home?authuser=0')
while True:
browser.refresh()
sourcetext=browser.page_source
searchword= ["Mobile", "standalone", "desktop"]
if (searchword in sourcetext):
print("FOUND!" searchword)
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
else:
print("not found")
sleep(5)
CodePudding user response:
Problem is here:
if (searchword in sourcetext):
'searchword' is not a single word but is a collection. when you try to use it like that (acting like it's a single word) only the first word will be searched.
So the solution here is to iterate over the 'searchword' and then search for the words inside it on the sourcetext.
Here's the last part of your code:
while True:
browser.refresh()
sourcetext=browser.page_source
searchword= ["Mobile", "standalone", "desktop"]
for word in searchword:
if word in sourcetext:
print("FOUND!" word)
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
else:
print("not found")
sleep(5)
CodePudding user response:
You can use intersection function by converting string to array.
This code print intersect words between searchword and sourcetext.
print( set(sourcetext.split()).intersection(searchword) )
CodePudding user response:
Thanks to @JCaesar, it worked. But I have no idea of why it worked.
while True:
browser.refresh()
sourcetext=browser.page_source
searchword= ["Starwars", "Citizen"]
for word in searchword:
if word in sourcetext:
print("FOUND!" word)
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
else:
print("not found")
sleep(5)
