Python Forum

Full Version: How to find a specific word in a webpage and How to count it.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am doing a project.
I don't know how to find a given word in a webpage and I want to count the occurrence of the given word.
I need a help in doing it.
I don't know how to code the process.
Can anybody give me correct code?

THIS IS MY CODE TILL NOW:
import requests
from bs4 import BeautifulSoup
url = input("Enter the url: ")
r = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')
import requests
from bs4 import BeautifulSoup

def count_words(url, the_word):
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content, 'lxml')
    words = soup.find(text=lambda text: text and the_word in text)
    print(words)
    return len(words)


def main():
    url = 'https://python-forum.io/Thread-How-to-find-a-specific-word-in-a-webpage-and-How-to-count-it'
    word = 'code'
    count = count_words(url, word)
    print('\nUrl: {}\ncontains {} occurrences of word: {}'.format(url, count, word))

if __name__ == '__main__':
    main()
Sorry dude, can you give me a simple code
Quote:Sorry dude, can you give me a simple code
That's pretty simple, don't know what you expect
This should work, in a half-ass sense.
import requests
from bs4 import BeautifulSoup
url = input("Enter the url: ")
r = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')
g = list(soup)
t = 0
for x in range (len(g)-5):
    
    if g[x]+g[x+1]+g[x+2]+g[x+3]+g[x+4]+g[x+5]==" code ":
        t = t+1
print (t)
(Feb-08-2018, 02:03 AM)pratheep Wrote: [ -> ]Sorry dude, can you give me a simple code

I want a code without using
def
(Feb-08-2018, 01:21 PM)pratheep Wrote: [ -> ]
(Feb-08-2018, 02:03 AM)pratheep Wrote: [ -> ]Sorry dude, can you give me a simple code

I want a code without using
def

you can remove the functions and do the same thing. In my opinion it reduces the readability though.
import requests
from bs4 import BeautifulSoup

url = 'https://python-forum.io/Thread-How-to-find-a-specific-word-in-a-webpage-and-How-to-count-it'
the_word = 'code'
r = requests.get(url, allow_redirects=False)
soup = BeautifulSoup(r.content, 'lxml')
words = soup.find(text=lambda text: text and the_word in text)
print(words)
count =  len(words)
print('\nUrl: {}\ncontains {} occurrences of word: {}'.format(url, count, the_word))
Without BeautifulSoup:

requests.get('http://golem.de').text.count('word')
When you say "in a webpage" do you mean in the text of the page, or in the source code also?
(Feb-08-2018, 01:53 PM)DeaD_EyE Wrote: [ -> ]Without BeautifulSoup:

requests.get('http://golem.de').text.count('word')

This would count also derivatives like 'wordless' in this example case
Pages: 1 2