尝试使用 curl 时获取菱形替代代码

Getting diamond altcodes when trying to use curl

提问人:Zoey Malkov 提问时间:10/20/2023 更新时间:10/20/2023 访问量:27

问:

我正在尝试获取此页面的内容:https://www.realestate.com.au/nsw/karuah-2324/

但是,当我使用以下代码获取页面内容时,我得到以下结果:

�]R�r�@����wSlg��-�RSƼt���l��鿷I��鋤#�t�H2n����p�ٳ"����Y�� ��A�uF�u�JT��n��q��_�Ϡ_V'��Wi��:���[|c�w�%�iVbt��^�>:��������d��U��.�������{C��H�H��zk��<�E�8 ����p"�ST�H$$��D�Bi��i�,i��R�9]1H��1������>�����9�`��Z�'��ӽ���i�~�6�)h#��g��ԟ�t������^W���捠�H8Q�]�`�x��`0�>�|J&���DEGPms�>-z�U[h��ˠh/W�<��N�� �7�H(:rYa���-X{R\��� Go�|\DK���O�g�+�`��}g'{�����<�����#�8�6�

我的代码:

<?php
function visit_url_with_curl($url) {
    $ch = curl_init($url);

    // Set cURL options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ignore SSL certificate verification (use in a safe environment)
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Ignore SSL certificate verification (use in a safe environment)

    // Execute cURL and store the response
    $response = curl_exec($ch);

    // Check for cURL errors
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    }

    // Close cURL session
    curl_close($ch);

    return $response;
}

$url = 'https://www.realestate.com.au/nsw/karuah-2324/';
$html = visit_url_with_curl($url);

// Print the HTML content of the page
echo $html;
?>

我能做些什么来正确获取内容?如果您访问该网站,您会看到 HTML 更干净,并且其中包含很多信息。

卷曲 php-curl

评论

1赞 Sammitch 10/20/2023
HTTP 响应的格式不正确,Curl 返回它仍然被 gzip 压缩,但您的浏览器“有帮助地”修复了这样的损坏响应。但即使你确实解码了它,它也只是一个插页式的 JavaScript 片段,让你四处奔波。这一切都是为了迷惑刮板。如果您想访问数据,您需要联系网站运营商。

答:

0赞 Dick Larsson 10/20/2023 #1

你得到的内容是gzip压缩的吗? 您需要向 cURL 添加额外的设置,以便正确读取网站中的编码内容。

尝试

curl_setopt($ch, CURLOPT_ENCODING, '');  // Accepts all encodings and allows cURL to decode them automatically

评论

0赞 Zoey Malkov 10/20/2023
现在它以可读文本的形式向我展示了一些东西,但它仍然不是 html 文本
0赞 Dick Larsson 10/20/2023
那个可读的文字,它是什么?
0赞 Zoey Malkov 10/20/2023
如果您访问 realestate.com.au/nsw/karuah-2324 并转到页面源代码,那里有很多信息。但是,此代码仅显示“<!DOCTYPE html><html><head></head><body><script>window。KPSDK={};KPSDK.now=性能类型!=='undefined'&&performance.now?performance.now.bind(performance):D ate.now.bind(Date);KPSDK.start=KPSDK.now();</script><script src=“/149e9513-01fa-4fb0-aad4-566afd725d1b/2d206a39-8ed7-437e-a3be-862e0f06eea3/ips.js”></script></body></html>'