количество подстрок на HTML странице с BeautifulSoup - PullRequest
0 голосов
/ 14 июля 2020

Мне нужно найти и подсчитать все слова «python» и «c ++» как подстроки в HTML коде с модулем BeautifulSoup. В википедии эти слова встречаются 1 и 9 раз соответственно. Почему мой код пишет 0 и 0?

from urllib.request import urlopen, urlretrieve

from bs4 import BeautifulSoup


resp = urlopen("https://stepik.org/media/attachments/lesson/209717/1.html") 

html = resp.read().decode('utf8') 

soup = BeautifulSoup(html, 'html.parser') 

table = soup.find('table', attrs = {'class' : 'wikitable sortable'})

cnt = 0

for tr in soup.find_all("python"):

    cnt += 1

print(cnt)

cnt1 = 0

for tr in soup.find_all("c++"):

    cnt += 1

print(cnt)

1 Ответ

1 голос
/ 14 июля 2020

Вы ошибаетесь, вам нужно использовать строковый аргумент для поиска любой строки

    
    # These will only work in case like these <b>Python</b>
    soup.find_all(string="Python")

    # Not in these <b>python</b> or <b>Python is best</b>
    #We can use regex to fix that they will work in substring cases 
    
    soup.find_all(string=re.compile("[cC]\+\+")) 
    soup.find_all(string=re.compile("[Pp]ython"))
...