-4

the output should be:

S&P 500 INDEX

3,824.68

the project link here: https://www.freecodecamp.org/news/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe/

import requests
from bs4 import BeautifulSoup
import ssl
url = "https://www.bloomberg.com/quote/SPX:IND"
html = requests.get(url)
soup = BeautifulSoup(html.content, "html.parser")
name_box = soup.find("h1", attrs={"class": "name"})
name = str(name_box)
print(name)
price_box = soup.find("div", attrs={"class": "price"})
price = str(price_box)
print(price)

2 Answers2

0

What happens?

You get output None be cause you wont find your expected data in the response.

Checking response, you will get this information:

We've detected unusual activity from your computer network. To continue, please click the box below to let us know you're not a robot.

May take a look at this alternative

MendelG
  • 14,885
  • 4
  • 25
  • 52
HedgeHog
  • 22,146
  • 4
  • 14
  • 36
0

HedgeHog is right that there is an issue with requesting the page. Here are some fixes to the scraping part of the program

name_box = soup.find("h1", attrs={"class": "companyName__99a4824b"})
name = name_box.text
print(name)
price_box = soup.find("span", attrs={"class": "priceText__1853e8a5"})
price = price_box.text
print(price)
James
  • 387
  • 1
  • 11
  • it sent this error: name = name_box.text AttributeError: 'NoneType' object has no attribute 'text' – Heba Allah. Hashim Jan 13 '21 at 15:28
  • Have you gotten past the issue of Bloomberg blocking you for unusual activity? If not then maybe look at this post for getting Bloomberg stock data: https://stackoverflow.com/a/58070435. Does the data have to come from scraping Bloomberg? You could also scrape from google – James Jan 17 '21 at 04:14