首页 > 解决方案 > 如何在php后台运行python网络爬虫

问题描述

我一直在试图弄清楚如何从 PHP 界面运行 python 网络爬虫。网络爬虫将数据放入 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++;
 }?>

标签: phppythonmysqlmysql-workbench

解决方案


推荐阅读