Getting garbage output when scraping a webpage in

2020-04-10 02:07发布

I am trying to get the contents of a page from Amazon using file_get_html() but the output comes with weird characters on echo. Can anyone please explain how can I resolve this issue?

I also found the following two related questions on Stack Overflow but they did not solve my issue. :)

  1. file_get_html() returns garbage
  2. Uncompress gzip compressed http response

Here is my code:

$options = array(
'http'=>array(
    'header'=>
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
            "Accept-language: en-US,en;q=0.5\r\n" .
            "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n"
   )
); 
$context = stream_context_create($options);

$amazon_url = 'https://www.amazon.com/my-url';
$amazon_html = file_get_contents($amazon_url, false, $context);

Here is the output I get:

��T]o�6}��`���0��݊-��"[�bh�tN�b0��.%%�$P��@�(Ų�� ������F#����A�

about 115k characters like this show up in the browser window.

These are my new headers:

$options = array(
'http'=>array(
    'header'=>
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
            "Accept-language: en-US,en;q=0.5\r\n"
   )
); 

Will using cURL resolve this issue?

Update:

I tried cURL. Still getting the garbage output. Here are my response headers:

HTTP/1.1 200 OK
Date: Sun, 18 Nov 2018 20:29:28 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.5
X-Powered-By: PHP/7.2.5
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

Can anyone explain the negative votes?

  1. I did a research myself.
  2. Found some related questions on Stack Overflow which did not solve my problem.
  3. Provided all the information that I thought would be helpful.

What else should I include in the question?

Here is my whole code for curl at present. This is the URL I am scraping.

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $amazon_url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($handle);
curl_close($handle);

echo $data;

The output is just a bunch of characters I mentioned above. Here are my request headers:

Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cookie: AMCV_17EB401053DAF4840A490D4C%40AdobeOrg=-227196251%7CMCIDTS%7C17650%7CMCMID%7C67056225185486460220940124683302119708%7CMCAID%7CNONE%7CMCOPTOUT-1524907071s%7CNONE; mjx.menu=renderer%3ACommonHTML; _ga=GA1.1.2019605490.1529649408; csm-hit=adb:adblk_no&tb:s-3521C4J8F2EP1V0MMQEP|1542578145652&t:1542578146256
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache

These are from the Network Tab. The response headers are the same as I mentioned above.

Here is the output after adding curl_setopt($handle, CURLOPT_HEADER, 1); to my code:

HTTP/1.1 200 OK Server: Server Content-Type: text/html; charset=UTF-8 Strict-Transport-Security: max-age=47474747; includeSubDomains; preload x-amz-id-1: 7A162B8JKV6MGZQ3PCH2 Vary: Accept-Encoding,User-Agent,X-Amzn-CDN-Cache Content-Encoding: gzip x-amz-rid: 7A162B8JKV6MGZQ3PCH2 Cache-Control: no-transform X-Frame-Options: SAMEORIGIN Date: Sun, 18 Nov 2018 22:42:51 GMT Transfer-Encoding: chunked Connection: keep-alive Connection: Transfer-Encoding Set-Cookie: x-wl-uid=1a4u8+XgF+IhFF/iavy9mKZCAA0g4HiIYZXR8hKjxGtmOtBW+j67wGABv7ZOTxDRcab+7Qmpjqds=;

1条回答
神经病院院长
2楼-- · 2020-04-10 02:56

Here's the solution:

I ran into the same issue when scraping Amazon. Simply add the following option before sending your cURL request:

curl_setopt($handle, CURLOPT_ENCODING, 'gzip,deflate,sdch');
查看更多
登录 后发表回答