J'ai l'intention de gratter un site Web en utilisant BeautifulSoup. Je travaille sur le code HTML suivant:Comment supprimer l'espace redondant dans la sortie de BeautifulSoup
html =
<div id="article-body" itemprop="articleBody">
<p>
<span class="quote down bgQuote" data-channel="/quotes/zigman/170324/composite" data-bgformat="">
<a class="qt-chip trackable" data-fancyid="XNYSStockSLB" href="/investing/stock/slb?mod=MW_story_quote" data-track-mod="MW_story_quote">
SLB,
<span class="bgPercentChange">-3.04%</span>
</a>
</span>
reported late Thursday
<a href="/story/schlumberger-profit-falls-sharply-2016-10-20-174854654" class="icon none">higher third-quarter profit that beat targets and sales only slightly below estimates</a>
. Schlumberger’s results came a day after rival Halliburton Co.
<span class="quote down bgQuote" data-channel="/quotes/zigman/228631/composite" data-bgformat="">
<a class="qt-chip trackable" data-fancyid="XNYSStockHAL" href="/investing/stock/hal?mod=MW_story_quote" data-track-mod="MW_story_quote">
HAL,
<span class="bgPercentChange">-0.66%</span>
</a> """
Je veux obtenir un texte brut, sans espace superflu, je suivais la réponse par Twig mais SLB et -3,04% et aussi HAL et -0,66% sont encore placés dans différentes sorties favorable lines.My serait comme:
SLB, -3.04% reported late Thursday higher third-quarter profit that beat targets and sales only slightly below estimates. Schlumberger’s results came a day after rival Halliburton Co. HAL, -0.66% also posted higher-than-expected profit.
Il est mon code:
import urllib2
from bs4 import BeautifulSoup
import re
newsText = soap(html)
text = list(newsText.stripped_strings)
finalText = "\n\n".join(text) if descriptions else ""
re.sub(r'[\ \n]{2,}', '', finalText)
print finalText
Je suis très reconnaissant à l'avance.
Où est votre code actuel? –
J'ai ajouté mon code, merci pour votre commentaire. –
Où obtenez-vous Schlumberger Ltd.? –