есть ли функция для поиска строки на веб-странице по некоторой процентной разнице

#python #beautifulsoup

#python #beautifulsoup

Вопрос:

я пытаюсь найти некоторую строку на странице с помощью библиотеки bs4, и она работает хорошо, но есть ли какой-либо способ найти строку через процент разницы, подобный этому примеру: у нас есть эта строка : The Eggplant the Witch and the Wardrobe 720p AMZN WEB-DL DD 5 1 H 264-QOQ

и должен ли я найти эту строку : The Eggplant the Witch and the Wardrobe 720p AMZN WEB-DL DD5 1 H264-QOQ

часть моего кода на Python :

 from bs4 import BeautifulSoup as Wsoup

x = The Eggplant the Witch and the Wardrobe 720p AMZN WEB-DL DD 5 1 H 264-QOQ
scn_rls_soup = Wsoup(my_driver, "html.parser")  

found = scn_rls_soup.find(text=x)

print(found)
  

Комментарии:

1.docs.python.org/3/library/difflib.html ??

2. вы имеете в виду нечеткое сопоставление?

Ответ №1:

Попробуйте что-то вроде этого:

 from bs4 import BeautifulSoup as Wsoup
from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

x = "The Eggplant the Witch and the Wardrobe 720p AMZN WEB-DL DD 5 1 H 264-QOQ"
scn_rls_soup = Wsoup(my_driver, "html.parser")  

found = scn_rls_soup.findAll(text=True)
for text in found:
    if similar(x,text) > 0.8:
        print(text)