更改Javascript文件的相对URL [英] Changing relative URLs of Javascript files

查看:134
本文介绍了更改Javascript文件的相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Curl进行屏幕抓取,如下所示:

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;
?> 

然后我在HTML页面上回显内容.问题是,当我在控制台中查看时,由于分配给javascript文件的相对URL,我看到404错误.例如,如果URL为:somejavascriptfile.js在加载页面时,我的域名将像这样添加:http://mydomain/somejavascriptfile.js这些路径显然不正确.

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.

那我该怎么做才能获取js文件的实际URL?如果URL可以在正文中使用jQuery(拆分/替换)进行更改,但在这种情况下将无法使用.

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.

推荐答案

您可以添加 base 标记到已抓取的HTML.

You can add a base tag to the scraped HTML.

使用HTML解析器(如整洁)打开它,转到到头部的开头,并附加一个<base>标记.基本标记会将所有资源访问重定向到已知位置.

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 .

这篇关于更改Javascript文件的相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆