To achieve this in Python, you can use the re
module for regular expressions. Here’s a function that extracts all instances of ../lyrics.*html
from a given string and returns them in a list:
مقالات ذات صلة
pythonimport re
def extract_links(html_string):
pattern = r'\.\./lyrics/.*?\.html'
links = re.findall(pattern, html_string)
return links
# Example usage
html_string = """
Love's in Need of Love Today
Have a Talk with God
Village Ghetto Land
"""
links = extract_links(html_string)
print(links)
This function uses a regular expression pattern to find all occurrences of ../lyrics.*html
in the html_string
and returns them as a list. You can adjust the pattern to match your specific requirements.
المزيد من المعلومات
بالطبع! في هذا السياق، يمكنك استخدام المكتبة BeautifulSoup لتحليل واستخراج روابط HTML بشكل أسهل. إليك كيفية استخدامها في الحصول على الروابط التي تبدأ بـ ../lyrics
:
pythonfrom bs4 import BeautifulSoup
def extract_links(html_string):
soup = BeautifulSoup(html_string, 'html.parser')
links = []
for link in soup.find_all('a'):
href = link.get('href')
if href and href.startswith('../lyrics'):
links.append(href)
return links
# Example usage
html_string = """
Love's in Need of Love Today
Have a Talk with God
Village Ghetto Land
"""
links = extract_links(html_string)
print(links)
هذا الكود يستخدم BeautifulSoup لتحليل النص الـ HTML والبحث عن الروابط التي تبدأ بـ ../lyrics
داخل عناصر ، ثم يقوم بإضافة هذه الروابط إلى قائمة ويعيد القائمة كنتيجة.