因為有抓新聞的需求,但是不是每個新聞網都有提供RSS/ATOM訂閱,看過jqeury 可以直接抓取網頁內容,https://www.tutorialrepublic.com/jquery-tutorial/jquery-ajax-load.php,還沒開始,就發現了文中的這個
Note: Ajax request can be made only to the files that exist on the same web server that servers the page from which the Ajax request is sent, not to external or remote servers for security reasons. This is called same-origin policy.
剛好最近在iframe 別人網站時,常看到這類的錯誤
Refused to frame ‘https://tw.yahoo.com/’ because an ancestor violates the following Content Security Policy directive: “frame-ancestors ‘self’
Refused to display ‘https://mail.example.com/’ in a frame because it set ‘X-Frame-Options’ to ‘sameorigin’.
Invalid ‘X-Frame-Options’ header encountered when loading ‘https://mail.example.com/’: ‘http://itxxx.exmple123.com/’ is not a recognized directive. The header will be ignored.
所以查了一下同源政策
https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Headers/X-Frame-Options
才發現事情不是我想像的那麼簡單,原本想直接修改C:\Windows\System32\drivers\etc\hosts變成同網域的網站來抓資料,結果都失敗了
留言