简体   繁体   中英

I am getting error like AttributeError: 'NoneType' object has no attribute 'text'

import pandas as pd

import requests

from bs4 import BeautifulSoup

page = requests.get('https://www.adityaispat.com/vision-mission.html', verify=False)

#url = "https://www.aarti-industries.com/csr/"

#page = requests.get(url)

soup = BeautifulSoup(page.text, 'html.parser')

print(soup.find(class_="text").text) 

or

print(soup.find('td', attrs={'class': 'text'}).text)

Simple answer to your question is that find() method returns object of type NoneType and you're trying to read text from it. To prevent this, it's worth to check first if find() gives any result.

result = soup.find(class_="text")
if result:
    print(result.text)
else
    # other action

Another answer is question: what you're trying to find? Maybe your class and/or atrributes to search are defined incorrectly.

I in your case the true source of problem is request part. If you check page.status_code , you'll notice that it returns 406, that means request was unseccessful. Basing on this solution you need to use a different User-Agent. Also remember to check if request was successful. Siplme way is to use ok property.

page = requests.get('https://www.adityaispat.com/vision-mission.html', headers={"User-Agent": "XY"})
if page.ok:
    print('Request OK!')
    # process the page
else:
    print('Request Failed!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM