PHP Web Scraping: Get Web Content
To retrieve specific content from a webpage, you can use PHP’s cURL function to grab the webpage content, and then use regular expressions or other methods to extract the desired content. Here’s a simple example:
// 目标网页的 URL
$url = "https://www.example.com";
// 初始化 cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行 cURL 请求
$response = curl_exec($ch);
// 关闭 cURL
curl_close($ch);
// 使用正则表达式获取指定内容
$pattern = '/<div class="content">(.*?)<\/div>/s';
preg_match($pattern, $response, $matches);
// 输出获取到的内容
echo $matches[1];
In the code above, first, the content of the target webpage is retrieved using cURL. Then a regular expression /
(.*?)<\/div>/s is used to match the content between
and
, and store the matching result in $matches. Finally, the specified content is outputted. You can modify the regular expression and processing logic as needed to retrieve different content.