get_hypertext¶
- pyrcs.parser.get_hypertext(hypertext_tag, hyperlink_tag_name='a', md_style=True)[source]¶
Extract text content from an HTML tag while preserving and formatting hyperlinks.
This function iterates through the child nodes of a BeautifulSoup tag element, converting hyperlink tags into standardised Markdown format or plain-text web link references.
- Parameters:
hypertext_tag (bs4.element.Tag | bs4.element.PageElement) – The tag containing text and hyperlinked element targets.
hyperlink_tag_name (str) – The target tag name of the hyperlink. Defaults to
'a'.md_style (bool) – Whether to return the hyperlinks in Markdown style. Defaults to
True.
- Returns:
The fully combined text string with formatted hyperlink references.
- Return type:
str
Examples:
>>> from pyrcs.parser import get_hypertext >>> from pyrcs.line_data import Electrification >>> import bs4 >>> import requests >>> elec = Electrification() >>> assert isinstance(elec.catalogue, dict) >>> url = elec.catalogue[elec.KEY_TO_INDEPENDENT_LINES] >>> source = requests.get(url) >>> soup = bs4.BeautifulSoup(source.content, 'html.parser') >>> h3 = soup.find('h3') >>> assert isinstance(h3, bs4.Tag) >>> hypertext_tag = h3.find_all_next('p')[9] <p>Croydon Tramlink mast references can be found on the <a href="http://www.croydon-tra... >>> result_text = get_hypertext(hypertext_tag, md_style=True) >>> result_text 'Croydon Tramlink mast references can be found on the [Croydon Tramlink Unofficial Site...