Я написал фрагмент кода, который ищет компании на этом веб-сайте https://violationtracker.goodjobsfirst.org/ и загружает csv-результат страницы компании - см. Пример для Nike здесь: https://violationtracker.goodjobsfirst.org/prog.php?parent=&major_industry_sum=&offense_group_sum=&primary_offense_sum=&agency_sum=&agency_sum_st=&hq_id_sum=&company_op=starts&company=nike&major_industry%5B%5D=&case_category=&offense_group=&all_offense%5B%5D=&penalty_op=%3E&penalty=&govt_level=&agency_code%5B%5D=&agency_code_st%5B%5D=&pen_year%5B%5D=&pres_term=&free_text=&case_type=&ownership%5B%5D=&hq_id=&naics%5B%5D=&state=&city=)
Код работал долго, но теперь я не уверен, почему вместо загрузки CSV он загружает только временный файл? С сайтом все в порядке, так как я пытался вручную и смог загрузить csv.
Это мой код
df_all = []
supplier = ['Nike']
length = len(supplier)
##go to the website
for idx, i in enumerate(supplier):
rem = length-idx
print('This is index: ', idx, ', element: ', i, ', with remaining : ', rem, ' elements')
try:
driver = webdriver.Chrome(executable_path=r"C:\webdrivers\chromedriver.exe")
driver.get("https://www.goodjobsfirst.org/violation-tracker")
##find the iframe with the broweser
driver.switch_to_frame(0)
## Insert text via xpath ##
elem = driver.find_element_by_xpath("//*[@id='edit-field-violation-company-value']")
elem.send_keys(i)
elem.send_keys(Keys.RETURN)
time.sleep(10)
try:
##download the information from the relevant page
button = driver.find_element_by_xpath('//*[@id="content"]/div/div[2]/a[1]/img')
ActionChains(driver).move_to_element(button).click(button).perform()
##upload last csv in the download folder
list_of_files = glob.glob(r'C:\Users\~\Downloads\*.csv')
latest_file = max(list_of_files, key=os.path.getctime)
time.sleep(3)
df = pd.read_csv(latest_file)
print(df)
df_all.append(df)
driver.close()
if os.path.exists(latest_file):
os.remove(latest_file)
else:
print("The file does not exist")
except:
driver.close()
except:
pass
violation_tracker = pd.concat(df_all)
Что мне не хватает?