Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Error object has no attribute text
#1
Hi all,

i am new to Python and am following examples found here
https://medium.freecodecamp.org/how-to-s...46935d93fe
to try to scrap text and prices from certain website.

It returned error although i followed the steps. Not sure what happened.

My code as per below:

# import libraries
import requests
import urllib
from bs4 import BeautifulSoup

# specify the url
quote_page='http://www.bloomberg.com/quote/SPX:IND'

# query the website and return the html to the variable ‘page’
page = urllib.request.urlopen(quote_page)

# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page,'html.parser')

# Take out the <div> of name and get its value
name_box = soup.find('h1', attrs={'class':'companyName__99a4824b'})

name = name_box.text.strip # strip() is used to remove starting and trailing
print (name)
this the text to be scrapped
<h1 class="companyName__99a4824b">S&P 500 Index</h1>

traceback:
name = name_box.text.strip # strip() is used to remove starting and trailing
AttributeError: 'NoneType' object has no attribute 'text'

Appreciate your guidance.

Thank you.

HC
Quote
#2
look at what soup give you back,so no info about stock is given that's why you get NoneType error.
One of the lines:
<p>Your usage has been flagged as a violation of our <a href="http://www.bloomberg.com/tos" rel="noopener noreferrer" target="_blank">terms of service</a>
tos(Terms Of Service)
Quote:You shall not use or attempt to use any “scraper,” “robot,” “bot,” “spider,” “data mining,” “computer code,”
or any other automate device, program, tool, algorithm, process or methodology to access, acquire, copy, or monitor any portion of the Service
So maybe find site a that more friendly that this,there may be a way be around,but not worth the effort.
It's just stock info a lot of sites has this for free with API and JSON output.
ALPHA VANTAGE | List of sites.
buran likes this post
Quote
#3
If you need quotes, you can use iex:

#!/usr/bin/python3
import requests, json

url = "https://api.iextrading.com/1.0/stock/ibm/quote"
rsp = requests.get(url)
if rsp.status_code in (200,):
    str = rsp.text.strip() # json
    print(str)
    print()
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text + "\n\n")
Quote
#4
@heiner55:
  • Don't use str as variable name. It's built-in function and you override it.
  • rsp.json() will give you parsed json response
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 4 207 Nov-11-2019, 05:15 PM
Last Post: nazmulfinance
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 0 133 Nov-10-2019, 09:13 PM
Last Post: nazmulfinance
  form.populate_obj problem "object has no attribute translate" pascale 0 328 Jun-12-2019, 07:30 PM
Last Post: pascale
  AttributeError: 'Response' object has no attribute 'replace' Truman 12 4,502 Mar-20-2019, 12:59 AM
Last Post: ichabod801
  Error: module 'urllib' has no attribute 'urlopen' mitmit293 2 2,398 Jan-29-2019, 02:32 PM
Last Post: snippsat
  AttributeError: 'dict' object has no attribute 'is_active' (PyMongo And Flask) usman 0 1,051 Nov-20-2018, 09:50 PM
Last Post: usman
  500 error cannot deserialize json object SaiBhargav 0 667 Sep-18-2018, 09:53 PM
Last Post: SaiBhargav
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 4,575 Sep-14-2018, 12:56 PM
Last Post: bmccollum
  'ellipsis' object has no attribute 'lower' Bharath_AK 4 2,000 Mar-15-2018, 07:15 AM
Last Post: Bharath_AK
  AttributeError: 'NoneType' object has no attribute error vk7 1 3,275 Feb-02-2018, 08:36 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)