2017年3月28日火曜日

httpsに変わってWebスクレイピングエラー

phpで作成したWebスクレイピングのプログラムが動かなくなった。

htmlが何か変わったのかと思ってブラウザでHTMLを表示してみるが、変更点はないようだ。でも動かなくなった。何が悪いの?

phpで作成したWebスクレイピングのプログラムが動かなくなった。

Webスクレイピングのプログラムは以下のような感じでphpでcurlを使っている。

    $url = 'http://xxxx.xxxx.co.jp/xxxx/';
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_USERAGENT,
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.2)' );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $c = curl_exec( $ch )

とりあえず、curl_execの結果をprint文で表示してみる。

????どういうこと?何も表示されない。curl_execが失敗しているのか?

何がどうなってるのかわからないので、httpリクエストの応答がどうなっているのかtcpdumpコマンドで見てみる。以下のような感じ。

# tcpdump -i enp5s0 'src host xxxx.xxxx.co.jp and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' -A
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp5s0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:41:18.770237 IP xxxx.xxxx.co.jp.http > localhost.localdomain.47766: Flags [P.], seq 638153757:638154510, ack 2066669371, win 32, options [nop,nop,TS val 3132218298 ecr 3703437694], length 753
E..%.d@.8.      ..O.{.....P..&  t.{..;...  ......
.......~HTTP/1.1 301 Moved Permanently
Date: Tue, 28 Mar 2017 12:38:17 GMT
P3P: policyref="http://xxxx.xxxx.co.jp/w3c/p3p_jp.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: B=5silu9pcdkm9p&b=3&s=1u; expires=Fri, 29-Mar-2019 12:38:17 GMT; path=/; domain=.xxxx.co.jp
Location: https://xxxx.xxxx.co.jp/xxxx/
Cache-Control: private
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Age: 0
Connection: keep-alive
Via: http/1.1 edge2431.img.xxx.xxxx.co.jp (ApacheTrafficServer [c s f ])
Server: ATS

リダイレクトかよ。URL変わったのか。と思ったが変わってないぞ。どうなってんだ?と思って「Location: 」で返された文字列をコピーして、プログラム上にペーストして比較。

なんとhttpがhttpsになっていた。

ということで、Webスクレイピングのプログラムの取得するURLをhttpからhttpsに変更。

    $url = 'https://xxxx.xxxx.co.jp/xxxx/';
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_USERAGENT,
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.2)' );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $c = curl_exec( $ch )

動きましたよ。

動いたのは良いのだが、これでは通信内容が暗号化されているのでtcpdumpで応答を見ることができない。鬱陶しい世の中になってきたね。

0 件のコメント:

コメントを投稿