直接上代码,PHP中函数是这么写的
public function product()
{
//接受数据,检测参数
$params = input();
//测试返回内容
//return json($params);
//检测参数是否为空
// $validata = $this->validate($params,[
// 'product|商品名' => 'require|length:1,20',
// ]);
// if ($validata !==true){
// $this->error($validata);
// }
// //参数加密
//// $params['password'] = encrypet_password($params['password']);
$product = $params['product'];
$py_file = "C:/Users/Administrator/PycharmProjects/jd_crawler/test.py";
$py_exe = "C:/Program Files/JetBrains/Python/python.exe";
// return $product;
exec("$py_exe $py_file $product",$out);
print_r($out);
}
参数通过测试可以正常返回,但是调用py文件后,不管怎么改,返回都为空,我该怎么测试是否调用了文件?
这是测试的py文件代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys # 键盘按键操作
import time
import pymysql
from db import insert_or_update
from db import connect
import sys
def get_goods(driver):
try:
goods = driver.find_elements_by_class_name('gl-item')
for good in goods:
detail_url = good.find_element_by_tag_name('a').get_attribute('href')
p_name = good.find_element_by_css_selector('.p-name em').text.replace('\n', '')
price = good.find_element_by_css_selector('.p-price i').text
p_commit = good.find_element_by_css_selector('.p-commit a').text
msg = '''
商品 : %s
链接 : %s
价钱 :%s
评论 :%s
''' % (p_name, detail_url, price, p_commit)
sku = "'" + p_name + "'"
url = "'" + detail_url + "'"
price = "'" + price + "'"
commit = "'" + p_commit + "'"
print(msg, end='\n\n')
# print(datalist)
sql = "insert into s_product_base(sku,price,commit,url)value(" + sku + ',' + price + ',' + commit + ',' + url + ")";
connect() # 连接数据库,嵌套函数
insert_or_update(sql) # 插入数据
button = driver.find_element_by_partial_link_text('下一页')
button.click()
time.sleep(1)
get_goods(driver)
except Exception as e:
print(e)
pass
def spider(url, keyword):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(3) # 使用隐式等待
try:
input_tag = driver.find_element_by_id('key')
input_tag.send_keys(keyword)
input_tag.send_keys(Keys.ENTER)
get_goods(driver)
finally:
driver.close()
if __name__ == '__main__':
key = sys.argv[1]
print(key)
spider('https://www.jd.com/', keyword=key)