Reputation: 2870
I'm trying to extract all the element a
inside a class whose text is Full browser
with
import requests
from bs4 import BeautifulSoup
url = 'https://www.freethesaurus.com/great+adductor+muscle'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
main = soup.select('*:has(strong:contains("Full browser")) ~ div.list-holder a')
list_holder = [x['href'] for x in main]
print(list_holder)
Could you please elaborate on how to select such a
that appear after the element <li class="current">...</li>
? Such a
inside <li class="current">...</li>
are also excluded.
Upvotes: 0
Views: 39
Reputation: 195553
To select all <a>
tags after <li class="current">
you can select all <li>
and then check for previous sibling with .find_previous_sibling()
method:
import requests
from bs4 import BeautifulSoup
url = 'https://www.freethesaurus.com/great+adductor+muscle'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
for li in soup.select('div.heading-block:contains("Full browser") + div li:has(a)'):
if li.find_previous_sibling('li', class_='current'):
print('{:<45} {}'.format(li.a.text, li.a['href']))
Prints:
Great Adirondack Birding Celebration //acronyms.thefreedictionary.com/Great+Adirondack+Birding+Celebration
Great Adirondack Tool Co. //acronyms.thefreedictionary.com/Great+Adirondack+Tool+Co.
Great Adventure //acronyms.thefreedictionary.com/Great+Adventure
Great Affordable Accommodation New Zealand //acronyms.thefreedictionary.com/Great+Affordable+Accommodation+New+Zealand
Great African Rift Valley //www.thefreedictionary.com/Great+African+Rift+Valley
Great African Rift Valley //encyclopedia2.thefreedictionary.com/Great+African+Rift+Valley
Great Alaskan Accounting People //acronyms.thefreedictionary.com/Great+Alaskan+Accounting+People
Great albatross //www.thefreedictionary.com/Great+albatross
Great Alfold //www.thefreedictionary.com/Great+Alfold
Great Alfold //encyclopedia2.thefreedictionary.com/Great+Alfold
Great Alföld //www.thefreedictionary.com/Great+Alf%c3%b6ld
Great Alföld //encyclopedia2.thefreedictionary.com/Great+Alf%c3%b6ld
Great Allegheny Passage //acronyms.thefreedictionary.com/Great+Allegheny+Passage
Great Aloha Run //acronyms.thefreedictionary.com/Great+Aloha+Run
Great Altai //www.thefreedictionary.com/Great+Altai
great alveolar cells //medical-dictionary.thefreedictionary.com/great+alveolar+cells
Great America //acronyms.thefreedictionary.com/Great+America
Great America Leasing Corporation //acronyms.thefreedictionary.com/Great+America+Leasing+Corporation
Great American Adventure Club //acronyms.thefreedictionary.com/Great+American+Adventure+Club
Great American Aran Afghan //acronyms.thefreedictionary.com/Great+American+Aran+Afghan
Great American Aran Afghan Along //acronyms.thefreedictionary.com/Great+American+Aran+Afghan+Along
Great American Backyard Campout //acronyms.thefreedictionary.com/Great+American+Backyard+Campout
Great American Bake Sale //acronyms.thefreedictionary.com/Great+American+Bake+Sale
Great American Ball Park //acronyms.thefreedictionary.com/Great+American+Ball+Park
Great American Ballpark //acronyms.thefreedictionary.com/Great+American+Ballpark
Great American Baseball League //acronyms.thefreedictionary.com/Great+American+Baseball+League
Great American Bash //acronyms.thefreedictionary.com/Great+American+Bash
Great American Beer Festival //acronyms.thefreedictionary.com/Great+American+Beer+Festival
Great American Bike Race //acronyms.thefreedictionary.com/Great+American+Bike+Race
Great American Brass Band Festival //encyclopedia2.thefreedictionary.com/Great+American+Brass+Band+Festival
Upvotes: 2