Python Forum
How to find a specific word in a webpage and How to count it.
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find a specific word in a webpage and How to count it.
#1
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')
Reply
#2
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()
Reply
#3
Sorry dude, can you give me a simple code
Reply
#4
Quote:Sorry dude, can you give me a simple code
That's pretty simple, don't know what you expect
Reply
#5
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)
Reply
#6
(Feb-08-2018, 02:03 AM)pratheep Wrote: Sorry dude, can you give me a simple code

I want a code without using
def
Reply
#7
(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))
Recommended Tutorials:
Reply
#8
Without BeautifulSoup:

requests.get('http://golem.de').text.count('word')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
When you say "in a webpage" do you mean in the text of the page, or in the source code also?
Recommended Tutorials:
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beautiful Soap can't find a specific section on the page Pavel_47 1 2,436 Jan-18-2021, 02:18 PM
Last Post: snippsat
  How to fix looking specific word in a webpage BSOD 0 1,862 Jun-16-2020, 08:01 PM
Last Post: BSOD
  Flask-Sqlalchemy count products in specific category imawesome 2 29,925 Mar-12-2020, 08:14 PM
Last Post: imawesome
  How to get the href value of a specific word in the html code julio2000 2 3,217 Mar-05-2020, 07:50 PM
Last Post: julio2000
  How do I extract specific lines from HTML files before and after a word? glittergirl 1 5,114 Aug-06-2019, 07:23 AM
Last Post: fishhook
  [split] How to find a specific word in a webpage and How to count it. marpop 2 5,816 Mar-12-2019, 08:25 AM
Last Post: snippsat
  XML Parsing - Find a specific text (ElementTree) TeraX 3 4,078 Oct-09-2018, 09:06 AM
Last Post: TeraX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020