【发布时间】:2019-05-27 17:08:04
【问题描述】:
我有从网站下载文件的 python 脚本。当我从终端传递参数时,Python 脚本在后台运行并成功下载文件。但是当我从 PHP 文件传递参数时,脚本会运行但不会下载文件。
我在无头模式下使用 Selenium 和 Chrome-webdriver。
谁能帮我找出我做错了什么?我在 Ubuntu Linux OS 上运行脚本。
我的 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 运行时没有下载文件
【问题讨论】:
-
请出示相关代码。请说明遇到错误的位置。另请参阅How to create a Minimal, Complete, and Verifiable example。
-
是的,我已经更新了
标签: php python linux selenium chromium