Can't find string after a tag with BeautifulSoup in Python?
<p>In this HTML I want to get the string of it but no matter what I try it doesn't work (string = none)</p>

<pre><code>      &lt;a href="/analyze/default/index/49398962/1/34925733" target="_blank"&gt;
       &lt;img alt="" class="ajax-tooltip shadow radius lazy" data-id="acctInfo:34925733_1" data-original="/upload/profileIconId/default.jpg" src="/images/common/transbg.png"/&gt;
       Jue VioIe Grace
      &lt;/a&gt;
</code></pre>

<p>There's a few of these on the page and I tried this:</p>

<pre><code>print([a.string for a in soup.findAll('td', class_='tou')])
</code></pre>

<p>The output is just none.</p>

<p>EDIT: here is the entire page HTML, hope this helps, just to clarify, I need to find all instances like the one above and extract their string</p>

<p><a href="http://pastebin.com/4mvcMsJu" rel="nofollow">http://pastebin.com/4mvcMsJu</a></p>

<p>You need to select the <em>a</em> from the parent <em>td</em> and call <em>.text</em>, the text is inside the anchor which is a child of the td:</p>

<pre><code>print([td.a.text for td in soup.find_all('td', class_='tou')])
</code></pre>

<p>There obviously is a td with the class tou or you would not be getting a list with None:</p>

<pre><code>In [10]: html = """&lt;td class='tou'&gt;
          &lt;a href="/analyze/default/index/49398962/1/34925733" target="_blank"&gt;
       &lt;img alt="" class="ajax-tooltip shadow radius lazy" data-id="acctInfo:34925733_1" data-original="/upload/profileIconId/default.jpg" src="/images/common/transbg.png"/&gt;
       Jue VioIe Grace
      &lt;/a&gt;
      &lt;/td&gt;"""

In [11]: soup = BeautifulSoup(html,"html.parser")

In [12]: [a.string for a in soup.find_all('td', class_='tou')]
Out[12]: [None]

In [13]: [td.a.text for td in soup.find_all('td', class_='tou')]
Out[13]: [u'\n\n       Jue VioIe Grace\n      ']
</code></pre>

<p>You could also call .text on the td:</p>

<pre><code>In [14]: [td.text for td in soup.find_all('td', class_='tou')]
Out[14]: [u'\n\n\n       Jue VioIe Grace\n      \n']
</code></pre>

<p>But that would maybe get more than you want.</p>

<p>using your full html from pastebin:</p>

<pre><code>In [18]: import requests

In [19]: soup = BeautifulSoup(requests.get("http://pastebin.com/raw/4mvcMsJu").content,"html.parser")

In [20]: [td.a.text.strip() for td in soup.find_all('td', class_='tou')]
Out[20]: 
 [u'KElTHMCBRlEF',
 u'game 5 loser',
 u'Cris',
 u'interestingstare',
 u'ApoIlo Price',
 u'Zary',
 u'Adrian Ma',
 u'Liquid Inori',
 u'focus plz',
 u'Shiphtur',
 u'Cody Sun',
 u'ApoIIo Price',
 u'Pobelter',
 u'Jue VioIe Grace',
 u'Valkrin',
 u'Piggy Kitten',
 u'1 and 17',
 u'BLOCK IT',
 u'JiaQQ1035716423',
 u'Twitchtv Flaresz']
</code></pre>

<p>In this case <code>td.text.strip()</code> gives you the same output:</p>

<pre><code>In [23]: [td.text.strip() for td in soup.find_all('td', class_='tou')]
Out[23]: 
[u'KElTHMCBRlEF',
 u'game 5 loser',
 u'Cris',
 u'interestingstare',
 u'ApoIlo Price',
 u'Zary',
 u'Adrian Ma',
 u'Liquid Inori',
 u'focus plz',
 u'Shiphtur',
 u'Cody Sun',
 u'ApoIIo Price',
 u'Pobelter',
 u'Jue VioIe Grace',
 u'Valkrin',
 u'Piggy Kitten',
 u'1 and 17',
 u'BLOCK IT',
 u'JiaQQ1035716423',
 u'Twitchtv Flaresz']
</code></pre>

<p>But you should understand that there is a difference. Also the difference between <a href="http://stackoverflow.com/questions/25327693/difference-between-string-and-text-beautifulsoup">.string vs .text</a></p>

python beautifulsoup