0

I am a little new to Python, and I am trying to understand how to extract the 'title=' attribute from this code (below). I have been trying to use beautifulsoup for it but honestly anything that will work is good for me.

<a class="image-link" href="/new-jersey/communities/holiday-city-at-berkeley" title="Holiday City at Berkeley"><div class="lazyload pulse out exited" style="height:auto"><div class="placeholder"><svg class="svg-placeholder-component" height="100%" viewbox="0 0 400 225" width="100%"><use xlink:href="#lazyload-placeholder"></use></svg></div></div></a>

I have tried all[0].find_all('a', "title") and all[0].find_all("title"), with both returning '[]'.

<a class="image-link" href="/new-jersey/communities/holiday-city-at-berkeley" title="Holiday City at Berkeley"><div class="lazyload pulse out exited" style="height:auto"><div class="placeholder"><svg class="svg-placeholder-component" height="100%" viewbox="0 0 400 225" width="100%"><use xlink:href="#lazyload-placeholder"></use></svg></div></div></a>
TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • please share your code. What is _all_ for example? You can use [edit] to add your code. Also, see https://stackoverflow.com/questions/11205386/python-beautifulsoup-get-an-attribute-value-based-on-the-name-attribute/11205758 , https://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup , https://www.crummy.com/software/BeautifulSoup/bs4/doc/ Note you can use select instead of find (which seems to be the most commonly returned search result ) Those links detail how to find and extract a title attribute. Emphasis _attribute_ – QHarr May 25 '19 at 07:54

1 Answers1

1

You can use CSS selector to extract desired elements:

from bs4 import BeautifulSoup

html = '<a class="image-link" href="/new-jersey/communities/holiday-city-at-berkeley" title="Holiday City at Berkeley"><div class="lazyload pulse out exited" style="height:auto"><div class="placeholder"><svg class="svg-placeholder-component" height="100%" viewbox="0 0 400 225" width="100%"><use xlink:href="#lazyload-placeholder"></use></svg></div></div></a>'
soup = BeautifulSoup(html, 'lxml')

for a in soup.select('a[title]'):
    print(a['title'])

Prints:

Holiday City at Berkeley
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91