Python 如何使用BeautifulSoup和Selenium实现if语句

Python 如何使用BeautifulSoup和Selenium实现if语句,python,beautifulsoup,Python,Beautifulsoup,并非所有易趣物品都相同,因为某些页面使用的格式与其他页面不同。我希望我的代码找到“price”元素,如果它不存在,那么尝试另一种方法。我创建了下面的代码,但我想知道什么是更好的方法 item = driver.find_element_by_xpath('//*[@id="prcIsum"]').text.strip().split() if len(item.text) > 0: price = item.text item = driver.fi

并非所有易趣物品都相同,因为某些页面使用的格式与其他页面不同。我希望我的代码找到“price”元素,如果它不存在,那么尝试另一种方法。我创建了下面的代码,但我想知道什么是更好的方法

    item = driver.find_element_by_xpath('//*[@id="prcIsum"]').text.strip().split()
    if len(item.text) > 0:
        price = item.text
    item = driver.find_element_by_xpath('//*[@id="mm-saleDscPrc"]')
    if len(item.text) > 0:
        price = item.text
    else:
        price = ""

使用
Selenium
时,如果元素不存在,则会引发错误,因此您必须使用try/except

import selenium.webdriver

url = 'https://toscrape.com/'
url = 'http://books.toscrape.com/'

driver = selenium.webdriver.Firefox()
driver.get(url)

try:
    item = driver.find_element_by_xpath('//tag').text.strip()
except Exception as ex:
    print(ex)
    try:
         item = driver.find_element_by_xpath('//a').text.strip()
    except Exception as ex:
         print(ex)
         item = ''

print(item)    
使用
BeautifulSoup
您可以获得
None
(或空列表),因此您必须在获得文本之前检查它

import selenium.webdriver

url = 'https://toscrape.com/'
url = 'http://books.toscrape.com/'

driver = selenium.webdriver.Firefox()
driver.get(url)

from bs4 import BeautifulSoup as BS

soup = BS(driver.page_source, 'html.parser')


item = soup.find('tag')
if item:
    item = item.get_text(strip=True)
else:
    item = soup.find('a')
    if item:
        item = item.get_text(strip=True)
    else:
        item = ''

print(item)

或者您可以尝试在
ttry/except

中获取文本。也许您想使用易趣API而不是删除他们的HTML?在Selenium上,当对象不存在时,它可能会引发错误,因此您可能必须使用
try/except
来捕获它。在其他模块中,
xpath()
可能会给出空列表或
None
,因此您必须在获取
之前检查它。text
@Tomalak,我正在避免使用API,因为我正在尝试学习WebScraping。