您好,我试图从php在virtualenv中运行python脚本,但我也做不到。
我想在我的输入php中插入一个社交网络,我想在我的php页面中获得链接
我的php代码和python脚本位于不同的文件夹中
这是我的php代码:
<!DOCTYPE html>
<html>
<body>
<h1>Search Yahoo link</h1>
<form method="post" action="#">
<input type="text" name="social_network" placeholder="">
<input type="submit" name="Search" value="Go">
</form>
<?php
if (isset($_POST['Search'])) {
$item=$_POST['social_network'];
#$tmp = exec("python php_python.py $item"); This line it works if python script isnt in Virtual environment (Outside)
$tmp = exec("cd C:/Users/admin/Desktop/python venv\scripts\activate.bat python yahoo.py $item");
echo $tmp;
}
?>
</body>
</html>
Python代码:
from selenium import webdriver
import time, sys
social_network = int(sys.argv[1]) # will receive values like: Facebook, Instagram, twitter, etc
driver= webdriver.Chrome()
driver.get('https://espanol.yahoo.com/?p=us')
#Search Social Network
driver.find_element_by_xpath('//*[@id="uh-search-box"]').send_keys(social_network)
driver.find_element_by_xpath('//*[@id="uh-search-button"]').click()
time.sleep(5)
link = driver.find_element_by_xpath('//*[@id="web"]/ol/li[2]/div/div[1]/div/span[1]').text
print(link)
driver.quit()