在 Python 代码中使用多个异常仍然会引发错误。如果有人知道请告诉我原因是什么:
import bs4
import time
import requests # pip install requests
from bs4 import BeautifulSoup # pip install bs4
from selenium import webdriver # pip install selenium
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager # pip install webdriver-manager
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
with webdriver.Chrome(service=Service(ChromeDriverManager().install()),
options=chrome_options) as driver: # Открываем хром
driver.get(f"https://www.stroyportal.ru/catalog/section-bolty-3128/st60/") # Открываем страницу
time.sleep(3) # Время на прогрузку страницы
soup = bs4.BeautifulSoup(driver.page_source, 'html.parser')
heads = soup.find('div', class_='col-12 col-xs-12 order-2 order-xs-2 order-sm-2 catalog_list_items').find_all(
'div', class_='catalog_list_item')
print(len(heads))
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
for i in heads:
try:
w = i.find_next('div', class_='format_img').find('a', href=True)
print('https://www.stroyportal.ru' + w['href'])
get_url = ('https://www.stroyportal.ru' + w['href'])
except:
get_url = "https://www.stroyportal.ru/catalog/section-bolty-3128/gayka-soedinitelnaya-m8-702404271/?popup=true"
break
stock = requests.get(get_url, headers=headers).text
moon = BeautifulSoup(stock, 'lxml')
try:
name = moon.find('div', class_='font-0 vertical-m').find('h1',
class_='d-inline inline-right-16 font-bold font-24')
print(name.text.strip())
except Exception:
name = moon.find('div', class_='col-7 col-sm-6 col-xs-12').find('h1', class_='font-22 font-bold')
print(name.text.strip())
except:
name = 'None'
print(name)
回溯(最近一次调用最后一次):文件“C:\Users\user\PycharmProjects\stroyportal_pars\main_3.py”,第 36 行,名称= Moon.find('div', class_='font-0 Vertical-m' ).find('h1', ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^ ^^^^^^ AttributeError:“NoneType”对象没有属性“find”
在处理上述异常的过程中,又出现了一个异常:
回溯(最近一次调用最后一次):文件“C:\Users\user\PycharmProjects\stroyportal_pars\main_3.py”,第 40 行,名称= Moon.find('div', class_='col-7 col-sm- 6 col-xs-12').find('h1', class_='font-22 font-bold') ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError:“NoneType”对象没有属性“find”
这是 BS4 中的常见错误。当在上一个搜索步骤中未找到某个元素(其值为 None)并且您尝试使用仅为对象树的真实元素定义的 BS4 方法时,就会发生这种情况。
我可以猜测:
我建议: