BeautifulSoup .get_text() element returns no results

Viewed 84

For some reason the code below does not return any results on different laptop's running the same python version 3.8.1 64bit and I cannot figure out why... I've tried 3 different laptops and chart is printing results only on one of them.

Does anyone have any ideas what might have been different or what's the reason the exact same code works on one machine, but not another?

import requests
from bs4 import BeautifulSoup

def check_facebook():

URL = 'https://downdetector.com/status/facebook/'

browser = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}

page = requests.get(URL, headers=browser)
soup = BeautifulSoup(page.content, 'html.parser')


chart = soup.find("div",{"class":"popover-container justify-content-center p-relative"}).script.get_text()


print("Facebook chart:", chart)

check_facebook()
1 Answers

Instead of .get_text() use .string property to get the contents of <script> tag:

import re
import requests
from bs4 import BeautifulSoup

def check_facebook():
    URL = 'https://downdetector.com/status/facebook/'
    browser = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}
    page = requests.get(URL, headers=browser)
    soup = BeautifulSoup(page.content, 'html.parser')
    chart = soup.find("div",{"class":"popover-container justify-content-center p-relative"}).script.string
    print('Status:', re.search(r"status: '(.*?)'", chart).group(1))
    print('Baseline:', re.search(r"baseline: (\d+)", chart).group(1))
    print('Company:', re.search(r"company: '(.*?)'", chart).group(1))
    print('Max:', re.search(r"max: (\d+)", chart).group(1))
    for x, y in re.findall(r"x: '(.*?)', y: (\d+)", chart):
        print(x, y)

check_facebook()

Prints:

Status: success
Baseline: 28
Company: Facebook
Max: 37
2020-07-24T18:29:28.479584-04:00 33
2020-07-24T18:44:28.479584-04:00 27
2020-07-24T18:59:28.479584-04:00 31

...and so on.
Related