Haven't found a solution particularly for this idea, so here's my question.
I have a list of keywords that I want to match with a scraped string from a website. This list is stored in an own Python file "Keywords" with the following content:
keywords = [
"FDA",
"Contract",
"Vaccine",
"Efficacy",
"SARS",
"COVID-19",
"Cancer",
"Exclusive",
"Explosive",
"Hydrogen",
"Positive",
"Phase"
]
The file is imported and to access this list, I can use Keywords.keywords.
#1 Match keywords with string:
I want to check whether the scraped string article_title = item.select_one('h3 small').find_next_sibling(text=True).strip() contains one of these keywords. If it does, I want to search for more content (got the code already). Else, I will return to the beginning of my for loop and search through the next title.
Here are examples of how the output for the string article_title looks like:
Global Water and Sewage Market Report (2021 to 2030) - COVID-19 Impact and Recovery
Blackbaud CEO Mike Gianoni Named One of 50 Most Influential by Charleston Business Magazine
Statement from Judy R. McReynolds on Signing of HR1319, the American Rescue Plan Act of 2021
What's the best way to match the list of keywords with the strings by only searching for whole words? I have found multiple approaches on SO, but they all seem to have flaws that people point out which confuses me.
#2 Store the found keyword in variable:
When matching with the keyword works, I store the found article_title variable and other variables in a database in case a keyword was found. However, I also want to store the keyword which caused the entry in my database. This allows me to know how many times each keyword was found. The variable where I store the found keyword should be called article_keyword. Is there a way to not only match the keywords with a string but also to store the found keyword? If yes, I would gladly appreciate help on how to do this.
If the provided information is not enough, let me know by a comment and I will add the full code. Just left it out for reasons of shorting the questions.