2016-09-04 4 views
0

Pourquoi cela ne fonctionne-t-il pas, lorsqu'il y a <br> dans le texte? Je reçois un texte vide.Pas de texte pour <br> dans BeautifulSoup

opener = urllib2.build_opener() 
opener.addheaders = [('User-agent', 'Mozilla/5.0')] 
address = 'http://www.bbc.com' 
response = opener.open(address) 
html = response.read() 
soup = BeautifulSoup(html) 
snaptext = soup.find('p', attrs={'class': 'displaytext'}) 
print snaptext.string 

Un exemple serait:

<p> blahblahblah<br/>blah2blah2blah2<br/><p> 

S'il y a un <br> dans le texte, le résultat est Aucun

Répondre

-2

Comme vous pouvez le voir ici, br est pas le problème, c'est votre l'utilisation de .string, il retournera toujours None, parce qu'il n'a pas l'attribut .string. Vous voulez probablement utiliser .getText()

>>> x = bs.find('div', attrs={'id': 'forum-post-body-183'}) 
>>> x 
<div class="j-comment-body forum-post-body u-typography-format text" id="forum-post-body-183" itemprop="text"> 
<p>Let's try it! I will only replace Sir Finley with Ysera for late game pressing (and ev. win condition).<br>In edit of this comment i would report about results in casual battles (for start).</br></p> 
</div> 
>>> x.string 
>>> print(x.string) 
None 
>>> x.getText() 
"\nLet's try it! I will only replace Sir Finley with Ysera for late game pressing (and ev. win condition).In edit of this comment i would report about results in casual battles (for start).\n" 
+0

Cela a fonctionné! Merci. – Dzrte4gle