gpt4 book ai didi

mysql - 使用解析器将 HTML 内容解析到 MySQL 数据库中

转载 作者:行者123 更新时间:2023-11-29 14:51:57 25 4
gpt4 key购买 nike

我希望能够将网站中的特定内容解析到 mySQL 数据库中。例如现场http://allrecipes.com/Recipe/Fluffy-Pancakes-2/Detail.aspx我想解析到我的数据库(其中有一个表,其中包含RecipeName、Ingredients 1-10 列)。

所以基本上我的数据库将包含该食谱的名称和所有成分。无需编辑内容,只需按原样解析它们(即 3/4 杯牛奶),因为我在数据库中使用字符。

我到底该如何做呢?我一直在寻找一个预构建的解析器,似乎很难找到一个易于使用的解析器,因为我对编程相当陌生。当然,我可以手动输入值,但我想解析它们。

是否可以只解析此内容并编写一个包含 RecipieName、成分字符串的文件,然后我可以将其解析到数据库中?或者我应该直接将其写入数据库?我不确定如何直接将数据库连接到解析器,但我也许可以在网上找到一些信息。

基本上,我正在寻求有关如何准确执行此操作的帮助,因为我不太精通编程,而且这似乎比实际情况要复杂得多。

我现在使用 Java 作为我的主要语言,尽管我不能说我非常擅长它。但我应该能够理解基本概念。

关于使用什么解析器或如何执行此操作有什么建议吗?

谢谢!

最佳答案

这就是我在 PHP 中的做法。这几乎肯定不是最有效的方法,也没有经过调试。

function parseHTML($rawHTML){
$startPosition = strpos($rawHTML,'<div class="ingredients"'); //Find the position of the beginning of the ingredients list, return the character number.
$endPosition = strpos($rawHTML,'</div>',$startPosition); //Find the position of the end of the ingredients list, begin searching from the beginning of the list (found in step 1)
$relevantPart = substr($rawHTML,$startPosition,$endPosition); //Isolate the ingredients list
$parsedString = strip_tags($relevantPart); //Strip the HTML tags off of the ingredients list
return $parsedString;
}

尚待完成:您说您有一个包含 10 个独立成分列的 mySQL 数据库。这段代码将所有内容输出为一个大字符串。您必须更改 strip_tags($relevantPart)函数为 strip_tags($relevantPart,"<li>") 。这会让<li>标签通过。然后,您必须循环遍历每个 <li>标签,执行与此类似的功能。它应该不会太难,但是在没有正常运行的 PHP 服务器的情况下编写它我感到不舒服。

关于mysql - 使用解析器将 HTML 内容解析到 MySQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com