Я пытаюсь извлечь ссылку из форума со следующим python кодом. Пост содержит много html ссылок, и я пытаюсь найти специальную:
<a href="https://site.html" target="_blank" class="externalLink" rel="nofollow">Daily news <img src="https://site.html/pic.png" class="bbCodeImage LbImage" alt="[IMG]" data-url="https://site.html/pic.png"></a>
Вот мой код:
from bs4 import BeautifulSoup
import defs
import re
def find_link(soup ,date, section, URL):
#Find the right post
section = soup.find('li', {"data-author":"Ghostwriter"})
#Search the link inside the post
link = section.find(string=" Daily news ")
#Mark the whole html section
section_new = str(link.find_parents('a'))
#get the link
link_new = re.search("(?P<url>https?://[^\s]+)", section_new).group("url")
Проблема сейчас в том, что иногда до или после «Ежедневных новостей» нет места, и мой код не выполняется:
AttributeError: 'NoneType' object has no attribute 'find_parents'
Как я могу сделать свой код более гибким, например, с некоторыми подстановочными знаками. Например:
link = section.find(string="*Daily news*")
Большое спасибо!