我有从网站下载文件的python脚本。当我从终端传递参数时,Python脚本将在后台运行并成功下载文件。但是,当我从PHP文件传递参数时,脚本会运行,但不会下载文件。
我在无头模式下使用Selenium和Chrome-webdriver。
有人可以帮助我找出我做错了什么吗?我正在Ubuntu Linux操作系统上运行脚本。
我的PHP脚本
<form method="post" action="CsvDownload.php" >
<label>Store Id</label>
<input type="text" class="form-control" name="storeId" id="store_id">
<button type="submit" class="btn btn-success"> Submit</button>
</form>
<?php
ini_set('max_execution_time', 300);
$store_id = false;
if(isset($_POST['storeId'])){
$store_id = $_POST['storeId'];
}
$command = "python python_file.py -s $store_id";
$output = exec($command);
echo $output;
?>
我的Python脚本
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import time
import csv
import os
import glob
import random
from datetime import date
import psutil
import requests
import json
import sys
count=0
for arg in sys.argv:
if arg == "-s":
temp = sys.argv[count+1]
count+=1
store_id=int(temp)
def downloadCSV(store_id,user_name,user_password):
options = Options()
PROCNAME = "chromedriver" # to clean up zombie Chrome browser
download_url = "/var/www/mywebsite.com/public_html/python_files/"
options.add_experimental_option("prefs", {
"download.default_directory": download_url,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
options.add_argument("download.default_directory = /var/www/mywebsite.com/public_html/python_files")
options.add_argument('--headless')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'/usr/local/bin/chromedriver')
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_url}}
command_result = driver.execute("send_command", params)
#open amazon url
driver.get("https://www.amazonlogistics.in/comp/packageSearch")
driver.find_element_by_id('ap_email').send_keys(user_name)
driver.find_element_by_id ('ap_password').send_keys(user_password)
driver.find_element_by_id('signInSubmit').click()
time.sleep(4)
driver.get("https://www.amazonlogistics.in/comp/packageSearch")
time.sleep(3)
#select update status date
driver.find_elements_by_css_selector("input[type='radio'][value='deliveryDate']")[0].click()
time.sleep(3)
Select(driver.find_element_by_xpath("//select[@id='shipStatusIdList']"))
dropdown_options = select_box.options
select_box = Select(driver.find_element_by_xpath("//select[@id='shipStatusIdList']"))
x=[select_box.select_by_index(ele_index) for ele_index in range(len(dropdown_options)) ]
time.sleep(3)
driver.find_element_by_id('downloadAsCSV').click()
time.sleep(15)
driver.find_element_by_class_name('log-out').click()
time.sleep(3)
driver.quit()
y=[proc.kill() for proc in psutil.process_iter() if proc.name() == PROCNAME]
# check whether the process name matches
#get latest csv file
list_of_files = glob.glob(download_url+'/*')
latest_csv = max(list_of_files, key=os.path.getctime)
today = date.today()
new_file_name = "package_search_{}_10pm_{}_{}_{}.csv".format(store_id,today.day,today.month,today.year)
print(new_file_name)
os.rename(latest_csv,os.path.join(download_url,new_file_name))
data = {
"file_name":new_file_name,
"message":"file downloaded successfully",
"status_code": 200
}
print(json.dumps(data))
list_of_data=[(1,"username","password"),(3,"username2","password2").......]
if store_id:
for data in list_of_data:
if data[0]==store_id:
downloadCSV(data[0],data[1],data[2])
else:
print("not suceess")
else:
print("end")
我没有收到任何错误,但是从PHP运行时文件没有下载
答案 0 :(得分:1)
您可以检查您的下载URL写入权限吗?如果没有,请更改为755。
也可以尝试以下方法: $ command =“ python python_file.py -s $ store_id”; 做到:$ command =“ python python_file.py -s”。$ store_id;