Попробуйте:
import requests
from bs4 import BeautifulSoup
for i in range(6):
url = 'https://www.nairaland.com/search/ipob/0/0/0/{}'.format(i)
the_word = 'afonja'
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 = 0
if words:
count = len(words)
print('\nUrl: {}\ncontains {} occurrences of word: {}'.format(url, count, the_word))
РЕДАКТИРОВАТЬ после новых спецификаций.
Предполагая, что слово для подсчета такое же, как в URL, вы можете заметить, что слово подсвечивается встраницы, и распознается по span class=highlight
в html.
Таким образом, вы можете использовать этот код:
import requests
from bs4 import BeautifulSoup
for i in range(6):
url = 'https://www.nairaland.com/search/afonja/0/0/0/{}'.format(i)
the_word = 'afonja'
r = requests.get(url, allow_redirects=False)
soup = BeautifulSoup(r.content, 'lxml')
count = len(soup.find_all('span', {'class':'highlight'}))
print('\nUrl: {}\ncontains {} occurrences of word: {}'.format(url, count, the_word))
и вы получите:
Url: https://www.nairaland.com/search/afonja/0/0/0/0
contains 30 occurrences of word: afonja
Url: https://www.nairaland.com/search/afonja/0/0/0/1
contains 31 occurrences of word: afonja
Url: https://www.nairaland.com/search/afonja/0/0/0/2
contains 36 occurrences of word: afonja
Url: https://www.nairaland.com/search/afonja/0/0/0/3
contains 30 occurrences of word: afonja
Url: https://www.nairaland.com/search/afonja/0/0/0/4
contains 45 occurrences of word: afonja
Url: https://www.nairaland.com/search/afonja/0/0/0/5
contains 50 occurrences of word: afonja