I am trying to parse a specific href link from the following website: https://www.murray-intl.co.uk/en/literature-library.
Element i seek to parse:
<a class="btn btn--naked btn--icon-left btn--block focus-within" href="https://www.aberdeenstandard.com/docs?editionId=9123afa2-5318-4715-9783-e07d08e2e7cc&_ga=2.12911351.1364356977.1629796255-1577053129.1629192717" target="blank">Portfolio Holding Summary<i class="material-icons btn__icon">library_books</i></a>
However, using BeautifulSoup I am unable to obtain the desired element, perhaps due to cookies acceptance.
from bs4 import BeautifulSoup import urllib.request import requests as rq page = requests.get('https://www.murray-intl.co.uk/en/literature-library') soup = BeautifulSoup(page.content, 'html.parser') link = soup.find_all('a', class_='btn btn--naked btn--icon-left btn--block focus-within') url = link[0].get('href') url
I am still new at BS4, and hope someone can help me on the right course.
Thank you in advance!
Advertisement
Answer
To get correct tags, remove "focus-within"
class (it’s added later by JavaScript):
import requests from bs4 import BeautifulSoup url = "https://www.murray-intl.co.uk/en/literature-library" soup = BeautifulSoup(requests.get(url).content, "html.parser") links = soup.find_all("a", class_="btn btn--naked btn--icon-left btn--block") for u in links: print(u.get_text(strip=True), u.get("href", ""))
Prints:
... Portfolio Holding Summarylibrary_books https://www.aberdeenstandard.com/docs?editionId=9123afa2-5318-4715-9783-e07d08e2e7cc ...
EDIT: To get only the specified link you can use for example CSS selector:
link = soup.select_one('a:-soup-contains("Portfolio Holding Summary")') print(link["href"])
Prints:
https://www.aberdeenstandard.com/docs?editionId=9123afa2-5318-4715-9783-e07d08e2e7cc