'AttributeError: 'NoneType' object has no attribute 'text' in beautiful soup [closed]
My goal is to scrape the stock price of the company which user will enter in the input box, but I am getting this error.
When I'm running the code without input block it's running fine, I don't know what's wrong with it.
from bs4 import BeautifulSoup
import requests
inp=input('company name')+":nse"
url1="https://www.google.com/finance/quote/"+inp.upper()
print(url1)
main_links=requests.get(url).text
soups=BeautifulSoup(main_link,'lxml')
stock_price=soup.find('div',class_="YMlKec fxKbKc").text
print(stock_price.text)
Output:
company nametatamotors:nse
https://www.google.com/finance/quote/TATAMOTORS:NSE
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/nz/ccxxqn4x7rn2k4784tjpdd2c0000gn/T/ipykernel_2968/3667408284.py in <module>
5 soups=BeautifulSoup(main_link,'lxml')
6 stock_price=soup.find('div',class_="YMlKec fxKbKc")
----> 7 print(stock_price.text)
AttributeError: 'NoneType' object has no attribute 'text'
Solution 1:[1]
You have typos in your code, fix it
Example
from bs4 import BeautifulSoup
import requests
inp = input('company name ') + ":nse"
url = "https://www.google.com/finance/quote/" + inp.upper()
main_links = requests.get(url=url)
soups = BeautifulSoup(main_links.content, features='html.parser')
stock_price = soups.find('div', class_="YMlKec fxKbKc")
print(stock_price.text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 0m3r |
