Well, first you need to get the content of the webpage you want to search the links. I highly recommend using requests, a simple HTTP library for Python:
import requests
response = request.get(https://www.stubhub.com/new-york-rangers-tickets/performer/2764/)
This specific URL for some reason requires a User-Agent header, so you should send one on the request:
url = 'https://www.stubhub.com/new-york-rangers-tickets/performer/2764/'
user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0'
response = requests.get(url, headers={'User-Agent':user_agent})
Then you can start analyzing the content of the page using beautifulsoup4. You can use the method find_all
passing a compiled regular expression as the text
parameter to find all a
tags that contains a certain text:
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(response.content, "html.parser")
rangers_anchor_tags = soup.find_all("a", text=re.compile(r".*\bNew York Rangers at\b.*")
urls = [anchor["href"] for anchor in rangers_anchor_tags]
urls
, then, would be a list of URLs that the respective inner text of the anchor tag contains the string in question.