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.
#11
Oh, yes. Then let's to the regex-trick 101...

re.findall(r'\w+',requests.get('https://google.de').text).count('word')

The regex r'\w+' finds all complete words.

If you want to do complex stuff, prevent the use of regex.
Regex is not able to parse HTML.

Then you should use BeautifulSoup or other parsers for html.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
(Feb-08-2018, 02:05 PM)metulburr Wrote: When you say "in a webpage" do you mean in the text of the page, or in the source code also?
Only in the text

(Feb-08-2018, 01:53 PM)metulburr Wrote:
(Feb-08-2018, 01:21 PM)pratheep Wrote: 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))
Thanks solved my problem.
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,420 Jan-18-2021, 02:18 PM
Last Post: snippsat
  How to fix looking specific word in a webpage BSOD 0 1,848 Jun-16-2020, 08:01 PM
Last Post: BSOD
  Flask-Sqlalchemy count products in specific category imawesome 2 28,222 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,196 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,096 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,785 Mar-12-2019, 08:25 AM
Last Post: snippsat
  XML Parsing - Find a specific text (ElementTree) TeraX 3 4,049 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