Ich habe Inhalte auf einer Website, auf der bestimmte Schlüsselwörter und Schlüsselwörter mit anderen Inhalten verknüpft werden sollten. Ich möchte keine Links manuell in den Inhalt einfügen. Was ist der beste Weg, dies in Python zu tun, vorzugsweise ohne Verwendung von DOM-Bibliotheken?
Zum Beispiel habe ich diesen Text:
...And this can be accomplished with the Awesome Method. Blah blah blah....
Key-phrase: awesome method
Dies ist die gewünschte Ausgabe:
...And this can be accomplished with the <a href="/path/to/awesome-method">Awesome Method</a>. Blah blah blah....
Ich habe eine Liste solcher Schlüsselwörter und die entsprechenden URLs. Die Phrasen können in jedem Fall im Inhalt erscheinen, werden jedoch in der Schlüsselwortdefinition nur in Kleinbuchstaben geschrieben.
Derzeit verwende ich String-Find-Replace mit Kombinationen der Wörter, bei denen die Groß- und Kleinschreibung geändert wurde. Und es ist ziemlich ineffizient.
3 Antworten
Wie wäre es mit so etwas
for keyphrase, url in links:
content = re.sub('(%s)' % keyphrase, r'<a href="%s">\1</a>' % url, content, flags=re.IGNORECASE)
In Ihrem Beispiel könnten Sie dies beispielsweise tun
import re
content = "...And this can be accomplished with the Awesome Method. Blah blah blah...."
links = [('awesome method', '/path/to/awesome-method')]
for keyphrase, url in links:
content = re.sub('(%s)' % keyphrase, r'<a href="%s">\1</a>' % url, content, flags=re.IGNORECASE)
# content:
# '...And this can be accomplished with the <a href="/path/to/awesome-method">Awesome Method</a>. Blah blah blah....'
Schauen Sie sich das anchorman -Modul an, um Ihren Text in Hypertext umzuwandeln.
import anchorman
text = "...And this can be accomplished with the Awesome Method. Blah blah blah...."
links = [{'awesome method': {'value': '/path/to/awesome-method'}}]
markup_format = {
'tag': 'a',
'value_key': 'href',
'attributes': [
('class', 'anups')
],
'case_sensitive': False
}
a = anchorman.add(text, links, markup_format=markup_format)
print a
...And this can be accomplished with the <a href="/path/to/awesome-method"
class="anups">Awesome Method</a>. Blah blah blah....
Sie können die Positionen im Text durchlaufen und den neuen Text mit grundlegenden Zeichenfolgenoperationen erstellen:
import re
text = """And this can be accomplished with the Awesome Method. Blah blah blah"""
keyphrases = [
('awesome method', 'http://awesome.com/'),
('blah', 'http://blah.com')
]
new_parts = []
pos = 0
while pos < len(text):
replaced = False
for phrase, url in keyphrases:
substring = text[pos:pos+len(phrase)]
if substring.lower() == phrase.lower():
new_parts.append('<a href="%s">%s</a>' % (url, substring))
pos += len(substring)
replaced = True
break
if not replaced:
new_parts.append(text[pos])
pos += 1
new_text = ''.join(new_parts)
print(new_text)
Verwandte Fragen
Neue Fragen
python
Python ist eine dynamisch typisierte Mehrzweck-Programmiersprache mit mehreren Paradigmen. Es wurde entwickelt, um schnell zu lernen, zu verstehen, zu verwenden und eine saubere und einheitliche Syntax durchzusetzen. Bitte beachten Sie, dass Python 2 ab dem 01.01.2020 offiziell nicht mehr unterstützt wird. Fügen Sie für versionenspezifische Python-Fragen das Tag [python-2.7] oder [python-3.x] hinzu. Wenn Sie eine Python-Variante (z. B. Jython, PyPy) oder eine Bibliothek (z. B. Pandas und NumPy) verwenden, fügen Sie diese bitte in die Tags ein.