Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web scraping number

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'
like image 203
Ahmed El-Ghorory Avatar asked Mar 11 '26 21:03

Ahmed El-Ghorory


1 Answers

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
like image 124
Andrej Kesely Avatar answered Mar 13 '26 12:03

Andrej Kesely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!