p0s0731
p0s0731

Reputation: 165

Count The occurence of a word

I am new to Python and making use of requests in python . I am asked to do loggin on xyz.com which i am able to do . And extract all the Table contents which are link to discussion . in each of these link i need to find the occurence of "The" word . how should i proceed ?My code is given below

tags=content2.findAll("td",{'class':'topic starter'})
for i in tags:
    thread_link=i.find('a').get('href')
    print(thread_link)
    result3=session.post(thread_link)
    content3=bs4.Beautifulsoup(result3.text,'html.parser')
    tag3=content3.find("the",count+1) 
    print(count) 

I have to find the occurence of the in each link and print it!!

Upvotes: 1

Views: 74

Answers (2)

Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14689

You can use str.count, check here for more details

for i in tags:
    thread_link=i.find('a').get('href')
    result3=session.post(thread_link)
    count = result3.text.count("the")
    print(count) 

Upvotes: 3

Rajan Chauhan
Rajan Chauhan

Reputation: 1375

You are not doing this correctly. Your tag3 will be finding the tag. Also code is little messy. You can use regex for your cause. Here we will search for the in the result text.

import re
for i in tags:
    thread_link=i.find('a').get('href')
    print(thread_link)
    result3=session.post(thread_link)
    count=len(re.findall('\sthe\s',result3.text))
    print(count) 

Upvotes: 1

Related Questions