【发布时间】:2010-03-26 12:09:33
【问题描述】:
如何使用 cURL 筛选网站并在特定 div 中显示数据?
【问题讨论】:
-
(建议) 尝试使用 DOM 和 XPath 来代替。
标签: php curl screen-scraping
如何使用 cURL 筛选网站并在特定 div 中显示数据?
【问题讨论】:
标签: php curl screen-scraping
使用 cURL 下载页面(文档中有很多examples)。然后使用 DOM Parser,例如 Simple HTML DOM 或 PHPs DOM 从 div 元素中提取值。
【讨论】:
使用 cURL 下载后,使用XPath 选择 div 并提取内容。
【讨论】:
一个可能的替代方案。
# We will store the web page in a string variable.
var string page
# Read the page into the string variable.
cat "http://www.abczyx.com/path/to/page.ext" > $page
# Output the portion in the third (3rd) instance of "<div...</div>"
stex -r -c "^<div&</div\>^3" $page
此代码在 biterscripting 中。我使用 3 作为样本来提取第三个 div。如果要提取包含字符串“ABC”的 div,请使用此命令语法。
stex -r -c "^<div&ABC&</div\>^" $page
看看这个脚本http://www.biterscripting.com/helppages/SS_ExtractTable.html。它展示了如何在元素嵌套时提取元素(div、表格、框架等)。
【讨论】:
使用 cURL GET 请求获取网站内容。 curl_exec manual page 上有一个代码示例。
使用正则表达式搜索您需要的数据。 preg_match manual page 上有一个代码示例,但您需要阅读 regular expressions 才能构建您需要的模式。正如Yacoby 提到的我没有想到的那样,一个更好的主意可能是使用 PHP 的 Simple XML 或 DOM 解析器检查 HTML 页面的 DOM。
在页面的 HTML 中输出您从正则表达式/解析器中找到的信息(在所需的 div 内。)
【讨论】: