一些些的code、一點點的Life,這就是生活。
有時候需要抓網站資料時,我以前有寫過curl的爬法,問題是常常會需要cookies阿 啥header等的資訊很麻煩阿,所以我發現了chrome透過開發者工具可以找到讓他copy轉成curl的bash資料。
而這bash資料其實直接在linux的cmd上就可以直接跑了
如果你在linux上可以直接跑的話你就可以透過下列網站
https://incarnate.github.io/curl-to-php/
(他的github:https://github.com/incarnate/curl-to-php)
或是https://github.com/davidou123/bash-curl-to-php-curl
把bash的資料直接幫你改成php的程式碼。
一整個超級方便阿,但是有時候還是不知道為啥還是不行時。可以用無敵大法(一樣限定在linux主機跑)
1 2 3 4 5 6 | <?php $output = shell_exec("curl 'https://xx.xxx' -H 'Pragma: no-cache' -H 'Origin: xxxxxxxxxxxxxxxxxxxxxxxxx --compressed"); //echo <pre>$output</pre>; $obj = json_decode($output); print $obj->{'resultsView'}; // 12345 ?> |
其中
1 | curl 'https://xx.xxx' -H 'Pragma: no-cache' -H 'Origin: xxxxxxxxxxxxxxxxxxxxxxxxx --compressed |
這段請自己換成chrome複製出來的資料,這樣即可
不過 能不用exec這個語法就最好不要用拉,他太強大邪惡了,甚麼linux指令都可以直接下,安全性要注意就是
一個不務正業,甚麼科技產品都玩的網路宅宅。 If you want to find me,that's easy! I will always on the Internet. so just find me, follow me. I will show you everything fun.