J'utilise BeautifulSoup4
dans Python2.7
pour essayer de trouver tous les liens uniques dans un corps HTML.Python Beautifulsoup trouver des liens uniques
Donc, si le corps HTML a 3 du même lien, utilisez-en seulement un.
Mon code ressemble à ceci:
def extract_links_from_content(self):
content = "%s %s" % (self.html_body, self.plaintext)
url_regex = 'http[s]?://(?:[a-zA-Z]|[0-9]|[[email protected]&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
links = []
soup = BeautifulSoup(content, "html5lib")
for link in soup.findAll('a'):
if not link.get('no_track'):
target = link.get('href')
name = link.get('data-name')
link_text = unicode(link)
# If there is no target or a mailto link
# skip it, cause we can't track whats not there or an email link
if not target or target.startswith('mailto') or '{' in target:
continue
# If the target is a bookmark, skip it
if target.startswith('#'):
continue
target = re.search(url_regex, target)
if target:
links.append({
'name': name,
'target': target.group()
})
return links
Alors, quelle est exactement votre question? –