1

I am trying to extract the value 'Netcare Ltd', but can't make it work.

<h1 class="secondary-header">
<span class='marketNameHeader'>Netcare Ltd</span>
<span class='diaryNoteAddButtonContainer'></span>
</h1>

I have tried the following (and various other options), but not sure where I am going wrong. Any ideas what I am missing?

name = soup.find('span', attrs={'class':'marketNameHeader'}).text
P-Gn
  • 23,115
  • 9
  • 87
  • 104
manjeetss
  • 43
  • 6
  • take a look here https://stackoverflow.com/questions/5041008/how-to-find-elements-by-class – efirvida Jul 17 '17 at 19:42
  • Is there an error, or is it not outputting the proper value, could you tell us what about this is wrong? – Professor_Joykill Jul 17 '17 at 19:44
  • Also looking at the BS4 documentation, you will want to use the method `get_text()` as just doing `.text` does not seem to be referenced there. – Professor_Joykill Jul 17 '17 at 19:46
  • @Professor_Joykill I get the following error message when running the code: Traceback (most recent call last): File "IGIndexDataScrape_Daily_v0.3.py", line 42, in name = soup.find('span', attrs={'class':'marketNameHeader'}).text AttributeError: 'NoneType' object has no attribute 'text' – manjeetss Jul 17 '17 at 19:49
  • @manjeetss If you have the error please edit your question to include it, also, as I mentioned, you should use the `get_text()` method as `.text` is not mentioned in the BS4 documentation. – Professor_Joykill Jul 17 '17 at 19:51
  • @Professor_Joykill Thank you. I have tried the following, but still got the error: name = soup.find('span', attrs={'class':'marketNameHeader'}).get_text() Error message: Traceback (most recent call last): File "IGIndexDataScrape_Daily_v0.3.py", line 42, in name = soup.find('span', attrs={'class':'marketNameHeader'}).get_text() AttributeError: 'NoneType' object has no attribute 'get_text' – manjeetss Jul 17 '17 at 19:52

1 Answers1

0

remove attrs, use .find to get first element and .find_all for all element

name = soup.find('span', { "class" : "marketNameHeader" })
print(name.text)
ewwink
  • 18,382
  • 2
  • 44
  • 54
  • I have tried the code, but get the following error message: Traceback (most recent call last): File "IGIndexDataScrape_Daily_v0.4.py", line 50, in print(name1.text) AttributeError: 'NoneType' object has no attribute 'text' – manjeetss Jul 17 '17 at 21:06
  • Removed .text and then get the value 'None' printed, but not the actual value – manjeetss Jul 17 '17 at 21:08