Skip to content Skip to sidebar Skip to footer

Python Beautifulsoup Extract Specific Urls


Solution 2:

If you're using BeautifulSoup 4.0.0 or greater:

soup.select('a[href^="http://www.iwashere.com/"]')

Solution 3:

You could solve this with partial matching in gazpacho:

Input:

html = """\
<a href="http://www.iwashere.com/washere.html">next</a>
<span class="class">...</span>
<a href="http://www.heelo.com/hello.html">next</a>
<span class="class">...</span>
<a href="http://www.iwashere.com/wasnot.html">next</a>
<span class="class">...</span>
"""

Code:

from gazpacho import Soup

soup = Soup(html)
links = soup.find('a', {'href': "http://www.iwashere.com/"}, partial=True)
[link.attrs['href'] for link in links]

Which will output:

# ['http://www.iwashere.com/washere.html', 'http://www.iwashere.com/wasnot.html']

Post a Comment for "Python Beautifulsoup Extract Specific Urls"