I am hoping to get a count of how often a specific word shows on a given URL. I currently have a way to do this for a small set of URLs and a single word:
import requests
from bs4 import BeautifulSoup
url_list = ["https://www.example.org/","https://www.example.com/"]
#the_word = input()
the_word = 'Python'
total_words = []
for url in url_list:
r = requests.get(url, allow_redirects=False)
soup = BeautifulSoup(r.content.lower(), 'lxml')
words = soup.find_all(text=lambda text: text and the_word.lower() in text)
count = len(words)
words_list = [ ele.strip() for ele in words ]
for word in words:
total_words.append(word.strip())
print('\nUrl: {}\ncontains {} of word: {}'.format(url, count, the_word))
print(words_list)
#print(total_words)
total_count = len(total_words)
However, my hope is to be able to do this for a mapped set of words to their respective URLs as shown in the below data frame.
Target Word | Target URL |
---|---|
word1 | www.example.com/topic-1/ |
word2 | www.example.com/topic-2/ |
The output would ideally give me a new column with a count of how often the word shows on its associated URL. For example, how often 'word1' shows on 'www.example.com/topic-1/'.
Any and all help is much appreciated!