Issue
I'm working with python an a dictionaries that I have not able to solve it. I'm working with a dictionaries. I'm trying search every element of the the dictionary words
inside all medias.items() and then four print it out lines, two for medias[0]
and two for medias[1]
. Is there something I made wrong? Any idea to fix it? Thanks in advance.
import requests
import time
from bs4 import BeautifulSoup
headers = {
'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"}
def count_words(url, the_word):
r = requests.get(url, headers=headers)
return r.text.lower().count(the_word)
def main():
# url = 'https://www.nytimes.com/'
medias = {
'Los Angeles Times': ['http://www.latimes.com/'],
'New York Times': ['http://www.nytimes.com/']
}
word = 'trump'
words = ['Trump', 'Facebook']
print('--- Iniciando ---')
print('Hora: ', time.strftime("%X"))
for web_name, urls in medias.items():
for url in urls:
count = count_words(url, words)
print('La palabra {} aparece {} veces en el sitio del {}.'.format(words, count, web_name))
if __name__ == '__main__':
main()
Solution
You had a few errors that I fixed within your code:
First, you forgot to loop through your array when using your words, so I added a for loop at the very end of your code to loop through those words.
Secondly, after I did that your code was returning zero so I added .lower() to each word within count_words and then it worked.
import requests
import time
from bs4 import BeautifulSoup
headers = {
'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"}
def count_words(url, the_word):
r = requests.get(url, headers=headers)
return r.text.lower().count(the_word.lower())
def main():
# url = 'https://www.nytimes.com/'
medias = {
'Los Angeles Times': ['http://www.latimes.com/'],
'New York Times': ['http://www.nytimes.com/']
}
word = 'trump'
words = ['Trump', 'Facebook']
print('--- Iniciando ---')
print('Hora: ', time.strftime("%X"))
for web_name, urls in medias.items():
for url in urls:
for word in words:
count = count_words(url, word)
print('La palabra {} aparece {} veces en el sitio del {}.'.format(word, count, web_name))
if __name__ == '__main__':
main()
Answered By - jreiss1923
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.