Я пытаюсь выяснить, сколько раз газетная статья была опубликована на Facebook с Beautifulsoup.
Вот эта страница: https://www.lemonde.fr/international/article/2018/06/10/isole-trump-est-parvenu-a-imposer-son-agenda-au-cours-d-un-g7-tendu_5312382_3210.html
и вот как выглядит HTML:
<div class="fixed-header fixed-header-show">
<div class="fixed-header-content">
<a class="fixed-header-logo" href="/"></a>
<div class="fixed-header-title">Isolé, Trump est parvenu à imposer son agenda au cours d’un G7 explosif</div>
<div class="fixed-header-sharing-buttons">
<div data-sharewith="facebook" data-xiti-label="Partage::Facebook::Isolé, Trump est parvenu à imposer son agenda au cours d’un G7 explosif::header">Partager<span class="fixed-header-facebook-likes-counter"> (142)</span></div>
<div data-sharewith="twitter" data-xiti-label="Partage::Twitter::Isolé, Trump est parvenu à imposer son agenda au cours d’un G7 explosif::header">Tweeter</div>
<div data-sharewith="google-plus" data-xiti-label="Partage::Google+::Isolé, Trump est parvenu à imposer son agenda au cours d’un G7 explosif::header"></div>
<div data-sharewith="linkedin" data-xiti-label="Partage::Linkedin::Isolé, Trump est parvenu à imposer son agenda au cours d’un G7 explosif::header"></div>
<div data-sharewith="pinterest" data-xiti-label="Partage::Pinterest::Isolé, Trump est parvenu à imposer son agenda au cours d’un G7 explosif::header"></div>
</div>
</div>
</div>
Вот как я получаю источник страницы в виде строки:
try:
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(req,timeout=20)
except:
timeOut = True
print(url,'timed out')
if timeOut:
return "timeOut",[]
if 'text/html' in response.getheader('Content-Type') and not timeOut:
htmlBytes = response.read()
htmlString = htmlBytes.decode("utf-8")
self.feed(htmlString)
return htmlString, self.links
else:
return "",[]
Я ищу часть "(142)" (это могло измениться наживая веб-страница, когда вы на нее посмотрите), под:
<span class="fixed-header-facebook-likes-counter"> (142)</span>
Вот как я пытаюсь это сделать:
shares = BeautifulSoup(data, "lxml").find("span", {"class": "fixed-header-facebook-likes-counter"}).text
Но она возвращает пустую строку (и,интересно, а не «None», как то, когда Beautifulsoup ничего не находит).Что мне здесь не хватает?