我一直在尝试找出如何从php接口运行pythonwebscraper。webscraper将数据放入mysql数据库,php接口将数据回送到表中。我花了很多时间研究这个,但是数据从来没有出现在表中,即使我把数据保存在数据库中,重新加载接口,也没有数据显示。同样,当我在终端中调用python脚本,然后打开接口时,数据到达。有没有什么方法可以推荐人们这样做?第一个php脚本如下:
<!DOCTYPE HTML>
<html>
<body>
<?php
echo "3h test";
exec("python 3h.py");
?>
<form>
<input type="button" value="ViewCode" onclick="location="'python 3h.py'>
</form>
<a href="action.php">Run script.</a>
</body>
</html>
python爬虫是:
from urllib import urlopen
from bs4 import BeautifulSoup
import pymysql.cursors
# Webpage connection
page1 = urlopen("http://www.officialcharts.com/charts/singles- chart/19800203/7501/")
page2= urlopen("http://www.officialcharts.com/charts/")
# Grab title-artist classes and store in recordList
bsObj0 = BeautifulSoup(page1,"html.parser")
bsObj1 = BeautifulSoup(page2,"html.parser")
recordList0 = bsObj0.findAll("div", {"class" : "title-artist",})
recordList = recordList0 + bsObj1.findAll("div", {"class" : "title-artist",})
connection = pymysql.connect(host='localhost',
user='root',
password='******',
db='crawler database',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
for record in recordList:
title = record.find("div", {"class": "title",}).get_text().strip()
artist = record.find("div", {"class": "artist"}).get_text().strip()
sql = "INSERT INTO `mytable` (`title`,`artist`) VALUES (%s,%s)"
cursor.execute(sql,(artist,title))
connection.commit()
finally:
connection.close()
python脚本名为3h.py
最后一个php脚本是为了让我费心打字,这是核心:
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['itemName'].'</td>
<td>'.$row['itemCost'].'</td>
</tr>';
$no++;
}?>
暂无答案!
目前还没有任何答案,快来回答吧!