import bs4, requests
url = requests.get(f'https://www.marketwatch.com/investing/stock/aapl?mod=over_search')
soup = bs4.BeautifulSoup(url.text,'lxml')
print(soup.find("div",{"class": "intraday__data"}).find("h3",{"class": "intraday__price"}).find("bg-quote"))
Output:
<bg-quote> channel="/zigman2/quotes/202934861/composite,/zigman2/quotes/202934861/lastsale" class="value" field="Last" format="0,0.00" session="pre">141.97</bg-quote>
It gives me an error when I type .text in the last of the last line and here is the error. I want to extract the price, any ideas?
AttributeError Traceback (most recent call last)
soup = bs4.BeautifulSoup(url.text,'lxml')
print(soup.find("div",{"class": "intraday__data"}).find("h3",{"class": "intraday__price"}).find("bg-quote")).text
AttributeError: 'NoneType' object has no attribute 'text'
You're typing .text after print() and print returns always None. Do this instead:
print(
soup.find("div", {"class": "intraday__data"})
.find("h3", {"class": "intraday__price"})
.find("bg-quote")
.text
) # <-- NOT .text here!
Prints:
141.77
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With