1
我有一個html
代碼,顯示了谷歌趨勢的相關專題:使用CURL像iframe一樣提取HTML?
<iframe id="trends-widget-1" src='https://trends.google.com/trends/embed/explore/RELATED_TOPICS?req={"comparisonItem":[{"keyword":"stack","geo":"BR","time":"today 5-y"}],"category":0,"property":""}&tz=180&eq=geo=BR&q=stack' width="100%" frameborder="0" scrolling="0" style="border-radius: 2px; box-shadow: rgba(0, 0, 0, 0.12) 0px 0px 2px 0px, rgba(0, 0, 0, 0.24) 0px 2px 2px 0px; height: 384px;"></iframe>
現在,我想找到一個方法來拯救這個網站(以備將來使用...)。爲此,我嘗試使用CURL
:
$url = 'https://trends.google.com/trends/embed/explore/RELATED_TOPICS?req={"comparisonItem":[{"keyword":"stack","geo":"BR","time":"today 5-y"}],"category":0,"property":""}&tz=180&eq=geo=BR&q=stack';
$ch = curl_init();
$source = $url;
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$html = curl_exec($ch);
curl_close($ch);
echo $html;
問題? curl
示出了與此消息的谷歌頁面:
- 這是一個錯誤。您的客戶發佈了格式不正確或非法的請求。我們知道的就這些。
我怎樣才能避免這種類型的問題,並提取html?
查詢字符串中的URL參數需要通過['urlencode()'](http://php.net/urlencode)運行以避免錯誤的請求。您的瀏覽器自動執行此操作,但當您提供整個URL時,curl不會自動執行。 – drew010
@ drew010我現在試試這個,我收到一個空的網頁.... – TheDuck
你能否更新問題或評論與您的更改?直接在您的問題中複製網址並粘貼到瀏覽器中也會提供400.這適用於:'$ url ='https://trends.google.com/trends/embed/explore/RELATED_TOPICS?req='。 urlencode('{「compareItem」:[{「keyword」:「stack」,「geo」:「BR」,「time」:「today 5-y」}],「category」:0,「property」 「}')。 '&TZ = 180當量=地理= BR&Q =棧';' – drew010