pandas 没有结果时出现Panda python Eval错误

tuwxkamq  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(170)

寻找一些帮助与我的代码。我运行 selenium 循环关键字搜索和使用Pandas获得表搜索结果,但当一个搜索词没有返回任何结果,我会得到一个错误,如果它有结果,但没有匹配的椭圆形过滤器它继续运行。
示例:一旦搜索到“Cedarrest”,将输出Pandas。错误。未定义变量错误:未定义名称“描述”。
这是我的代码,我如何修复,使它会跳过搜索条件,没有结果?

import time
import requests
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

class KW_POST_BOT(object):
    def __init__(self, browser, search_engine_url, kw_list, package):
        self.browser = browser
        self.package = package
        self.search_engine_url = search_engine_url
        self.kw_list = kw_list

    def main(self):
        self.browser.get("https://www.dnv.org/building-development/look-building-and-trades-permits")
        ###   MAIN LOOP
        #   INJECT KEY WORD LIST

        #   LOOPS THROUGH KW_LIST AS KW
        for kw in self.kw_list:
            print("*" * 30)
            print("Searching " + kw)
            print("*" * 30)
            print("Finding Data Table")
            print("-" * 30)
            #   WAIT 3 SECONDS
            time.sleep(3)
            #   LOCATE ELEMENT AND CLEAR
            elem = self.browser.find_element(By.XPATH,
                                             "/html/body/div[2]/div[1]/div[2]/div[2]/div/div/div/app-root/div/div[1]/div/div[1]/div/div/ng2-completer/div/input").clear()
            #   LOCATE ELEMENT AND SEND KEYS :  KW VARIABLE
            elem = self.browser.find_element(By.XPATH,
                                             "/html/body/div[2]/div[1]/div[2]/div[2]/div/div/div/app-root/div/div[1]/div/div[1]/div/div/ng2-completer/div/input").send_keys(
                kw)
            #   PRESS ENTER
            elem = self.browser.find_element(By.XPATH,
                                             "/html/body/div[2]/div[1]/div[2]/div[2]/div/div/div/app-root/div/div[1]/div/div[1]/div/div/ng2-completer/div/input").send_keys(
                Keys.ENTER)
            #   WAIT 3 SECONDS
            time.sleep(3)

            #   PRINT IN TERMINAL
            table_trs = self.browser.find_elements(By.XPATH, '//table[@id="case_table"]/tbody/tr')
            value_list = []
            for row in table_trs[1:]:
                value_list.append({
                    # 'Permit': row.find_elements(By.TAG_NAME, "td")[0].text,
                    'Address': row.find_elements(By.TAG_NAME, "td")[1].text,
                    # 'Value': row.find_elements(By.TAG_NAME, "td")[2].text,
                    # 'Contact': row.find_elements(By.TAG_NAME, "td")[3].text,
                    'Status': row.find_elements(By.TAG_NAME, "td")[4].text,
                    'Date': row.find_elements(By.TAG_NAME, "td")[5].text,
                    'Description': row.find_elements(By.TAG_NAME, "td")[6].text
                })

            df = pd.DataFrame(value_list)
            filtered_list = df[
                df.eval("Description.str.contains('New') & Date.str.contains('2018|2019|2020|2021|2022').values")]

            #   TURN THEWEB OBJECT INTO TEXT AND ENCODE IN UTF-8
            # table_data = table_data.text.encode("utf-8")
            print(filtered_list)

#   DEFINE KEW_WORDS LIST TO INPUT
key_words = ["Eldon", "Ruby", "Bracknell", "Pelly", "Sunset", "Edgewood", "Sycamore", "Lodge",
             "Virginia", "Loraine", "Kendal", "Emerald", "Dudley", "Highland", "Highland", "Montroyal", "Ranger",
             "Shirley", "Cedarcrest", "Lions", "Sunnycrest", "Beaumont", "Tudor", "Winona", "Canterbury",
             "Beaconsfield", "Hampshire", "Devon", "Essex", "Derby", "Belgrave", "Cheviot", "Parliament", "Ruskin",
             "Handsworth", "Rialto", "Belvedere", "Marineview", "Mapleridge", "Pheasant", "Ruthina", "Marigold",
             "Marigold", "Glenwood", "Timberline", "Ventura", "Monteray", "Greenway", "Valencia", "Hermosa", "Vienna",
             "Genoa", "Saville", "Granada", "Lucerne", "Verona", "Croydon", "Silverdale", "Lewister", "Langdale",
             "Quinton", "Carolyn", "Wavertree", "Wentworth", "Leovista", "Trenton", "Evergreen", "Chelsea", "Crystal",
             "Sylvan", "Alpine", "Bonita", "Palisade", "Blueridge", "Skyline", "Glencanyon", "Delmar", "Dolores",
             "Delbrook", "Linnae", "Teviot", "Belvista", "Prospect", "Primrose", "Edgewood", "Patterdale", "Newdale",
             "Crestwood", "Montroyal", "Glenview", "Arundel", "Ranger", "Capilano", "Salvador", "Grace", "East", "June",
             "Cliffridge", "Glenn"]

#   INSTANTIATE KW_POST_BOT AS bot
bot = KW_POST_BOT(webdriver.Firefox(), "https://www.dnv.org/building-development/look-building-and-trades-permits",
                  key_words, [])

bot.main()
cbeh67ev

cbeh67ev1#

你可以使用这样的try/except代码块,它首先运行代码,如果panda出现错误,它只会打印一条消息,然后继续下一个搜索词,从而避免程序停止

try:
    //your pandas code here
except pd.errors.UndefinedVariableError:
    print("No results")

https://docs.python.org/3/tutorial/errors.html?highlight=try%20except

相关问题