mysql - 将外部网站上的内容与 mySQL 数据库中的条目配对

标签 mysql html-parsing

tl;dr:我正在寻找一种方法来查找我们数据库中缺少信息的条目,从网站获取该信息并将其添加到数据库条目中。


我们有一个媒体管理程序,它使用 mySQL 表来存储信息。当员工下载媒体(视频文件、图像、音频文件)并将其导入媒体管理器时,他们假设还要复制媒体的描述(来自源网站)并将其添加到描述中在媒体管理器中。然而,这还没有为 文件完成。

文件名(例如 file123.mov)是唯一的,可以通过转到源网站上的 URL 访问该文件的详细信息页面:

website.com/content/file123

我们要从该页面抓取的信息有一个始终相同的元素 ID。

在我看来,这个过程是:

  1. Connect to database and Load table
  2. Filter: "format" is "Still Image (JPEG)"
  3. Filter: "description" is "NULL"
  4. Get first result
  5. Get "FILENAME" without extension)
  6. Load the URL: website.com/content/FILENAME
  7. Copy contents of the element "description" (on website)
  8. Paste contents into the "description" (SQL entry)
  9. Get 2nd result
  10. Rinse and repeat until last result is reached

我的问题是:

  1. 是否有可以执行此类任务的软件,或者这是否需要编写脚本?
  2. 如果编写脚本,最好的脚本类型是什么(例如,我可以使用 AppleScript 实现这个,还是需要用 java 或 php 等编写)

最佳答案

  1. Is there software that could perform such a task or is this something that would need to be scripted?

    我不知道有什么可以开箱即用地做你想做的事情(即使有,所需的配置也不会比滚动你自己的解决方案所涉及的脚本少得多)。

  2. 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/

相关文章:

mysql - 如何根据对表 X 的查询结果返回表 Y 的行?

php - 如何比较当前结果集与前一个结果集的值?

mysql - 如何从 'Case When' 条件开始计数

mysql - 在 MYSQL 中用 2 个坐标(以米为单位)计算地球上距离的最佳方法是什么?

php - 错误 : "SQLSTATE[28000] [1045] Access denied for user" on cPanel SQL database

php - 解析后在 <pre> 的文本中保留回车符

html - 为什么杂散的 </p> 结束标记会生成一个空段落?

python - 使用 rowspan 和 colspan 解析表

python - BeautifulSoup 使用 python 删除除白名单中的所有 html 标签,例如 "img"和 "a"标签

python - 缓慢的 html 解析器。如何提高速度?