Changing relative URLs of Javascript files

爱⌒轻易说出口 提交于 2019-12-25 02:58:01

问题


I'm screen scraping using Curl like this:

<?php
$url = "http://www.bbc.com/news/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?> 

And then I echo the content on an HTML page. The problem is that when I look in my console I see 404 errors because of relative URLs assigned to javascript files. For instance if the URL is: somejavascriptfile.js on loading the page my domain name is added like so: http://mydomain/somejavascriptfile.js These paths are obviously not correct.

So what can I do to get the actual URL of the js file instead? If the URLs where in the body I could use jQuery (split/replace) to alter but this wouldn't work in this case.


回答1:


You can add a base tag to the scraped HTML.

Open it with an HTML parser like tidy, go to the start of the head section and append a <base> tag. A base tag will redirect all resource access to a known location .



来源:https://stackoverflow.com/questions/15849084/changing-relative-urls-of-javascript-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!