tl;dr:我正在寻找一种方法来查找我们数据库中缺少信息的条目,从网站获取该信息并将其添加到数据库条目中。
我们有一个媒体管理程序,它使用 mySQL 表来存储信息。当员工下载媒体(视频文件、图像、音频文件)并将其导入媒体管理器时,他们假设还要复制媒体的描述(来自源网站)并将其添加到描述中在媒体管理器中。然而,这还没有为 千 文件完成。
文件名(例如 file123.mov)是唯一的,可以通过转到源网站上的 URL 访问该文件的详细信息页面:
website.com/content/file123
我们要从该页面抓取的信息有一个始终相同的元素 ID。
在我看来,这个过程是:
- Connect to database and Load table
- Filter:
"format"
is"Still Image (JPEG)"
- Filter:
"description"
is"NULL"
- Get first result
- Get
"FILENAME"
without extension)- Load the URL: website.com/content/
FILENAME
- Copy contents of the element
"description"
(on website)- Paste contents into the
"description"
(SQL entry)- Get 2nd result
- Rinse and repeat until last result is reached
我的问题是:
- 是否有可以执行此类任务的软件,或者这是否需要编写脚本?
- 如果编写脚本,最好的脚本类型是什么(例如,我可以使用 AppleScript 实现这个,还是需要用 java 或 php 等编写)
最佳答案
-
Is there software that could perform such a task or is this something that would need to be scripted?
我不知道有什么可以开箱即用地做你想做的事情(即使有,所需的配置也不会比滚动你自己的解决方案所涉及的脚本少得多)。
-
If scripted, what would be the best type of script (eg could I achieve this using AppleScript or would it need to be made in java or php etc.)
AppleScript 无法连接到数据库,因此您肯定需要添加一些其他东西。如果在 Java 和 PHP 之间做出选择(并且您同样熟悉两者),我绝对会为此目的推荐 PHP,因为涉及的代码会少得多。
您的 PHP 脚本看起来像这样:
$BASEURL = 'http://website.com/content/'; // connect to the database $dbh = new PDO($DSN, $USERNAME, $PASSWORD); // query for files without descriptions $qry = $dbh->query(" SELECT FILENAME FROM mytable WHERE format = 'Still Image (JPEG)' AND description IS NULL "); // prepare an update statement $update = $dbh->prepare(' UPDATE mytable SET description = :d WHERE FILENAME = :f '); $update->bindParam(':d', $DESCRIPTION); $update->bindParam(':f', $FILENAME); // loop over the files while ($FILENAME = $qry->fetchColumn()) { // construct URL $i = strrpos($FILENAME, '.'); $url = $BASEURL . (($i === false) ? $FILENAME : substr($FILENAME, 0, $i)); // fetch the document $doc = new DOMDocument(); $doc->loadHTMLFile($url); // get the description $DESCRIPTION = $doc->getElementsById('description')->nodeValue; // update the database $update->execute(); }
关于mysql - 将外部网站上的内容与 mySQL 数据库中的条目配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712724/