Zum Beispiel möchte ich das src-Attribut eines Tags mit id="updatable" aktualisieren. Ich habe das src-Attribut erfolgreich mit BeautifulSoup extrahiert, aber beim Versuch, es zu aktualisieren, stecke ich fest.
Hier ist mein aktueller Ansatz:
Code: Select all
import markdown
from bs4 import BeautifulSoup
import json
import chardet
def get_encoding_type(file_path):
with open(file_path, 'rb') as f:
sample = f.read(1024)
cur_encoding = chardet.detect(sample)['encoding']
return cur_encoding
with open("README.md", "r", encoding = get_encoding_type("README.md"),errors='ignore') as f:
markdown_content = f.read()
html_content = markdown.markdown(markdown_content)
parser = BeautifulSoup(html_content, "html.parser")
img_id = parser.find("img",{'id':'updatable'})
try:
img_source = img_id['src']
print(img_source)
except:
print("No image found")