php - 如何在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++;
}?>
解决方案
推荐阅读
- android - 点击 recyclerview 时隐藏浮动操作按钮 (FAB)
- django - Faker() 引发 Django ImproperlyConfigured 错误
- c# - 如何将int转换为枚举而不是使用switch
- regex - 正则表达式:在两侧的 2 个空格之间匹配单词
- python - 用小写字母、大写字母和数字对字典的键进行排序
- android - Android Studio - 如何使用我的 wifi 连接到互联网
- r - 如何以整洁的方式重新编码众多因子变量
- ruby - 在呼叫现场评估参数
- c# - 将 xml 反序列化为对象 C#(无法将 xml 加载到对象中)
- python - 如何将行添加到 pandas DataFrame 中的特定位置?