Finds all anchor tags and parses the href attribute. example attribute: `tel:+45651112331` or possiby the href attribute itself.
(soup: BeautifulSoup)
| 175 | |
| 176 | |
| 177 | def parse_phone_numbers(soup: BeautifulSoup) -> list[str]: |
| 178 | """ |
| 179 | Finds all anchor tags and parses the href attribute. |
| 180 | example attribute: `tel:+45651112331` or possiby the href attribute itself. |
| 181 | """ |
| 182 | tags = soup.find_all('a') |
| 183 | numbers = set() |
| 184 | for tag in tags: |
| 185 | if tag.has_attr('href') and 'tel:' in tag['href']: |
| 186 | number = tag['href'].split('tel:', 1)[1] |
| 187 | try: |
| 188 | if phonenumbers.is_valid_number(number): |
| 189 | numbers.add(number) |
| 190 | except Exception as e: |
| 191 | logging.debug(e) |
| 192 | pass |
| 193 | |
| 194 | try: |
| 195 | if phonenumbers.is_valid_number(tag['href']): |
| 196 | numbers.add(tag['href']) |
| 197 | except Exception as e: |
| 198 | logging.debug(e) |
| 199 | pass |
| 200 | |
| 201 | return list(numbers) |