I need to extract all elements with /Company in them, like this: [/Company/apple-hospitality-reit-inc, /Company/apple-in, /Company/maui-land-pineapple-co-inc, /Company/pineapple-energy-inc]. How can I do this?
You can use a list comprehension to achieve this. Here’s the modified code:
from bs4 import BeautifulSoup
import requests
CompanyNOT = "apple"
url = "https://www.annualreports.com/Companies?search="
page_to_scrape = requests.get(url+CompanyNOT)
soup=BeautifulSoup(page_to_scrape.content,"html.parser")
urls = [link['href'] for link in soup.find_all('a', href=True) if '/Company' in link['href']]
print(urls)