[英]I want to download all the links on all the pages of a website, the number of pages is very very large
[英]I want to Add a prefix on all website href links
我想在所有網站href鏈接上添加前綴-例如,-i要添加前綴-xyz.com/url? 在所有鏈接上-假設href鏈接的網址是-fpqr.com。在添加該前綴后,其顯示-http://www.xyz.com/url? http ://www.fpqr.com
我寫了這段代碼:
var prefix = "http://www.xyz.com/url?";
$('a').each(function(){
var currentHref = $(this).attr('href');
$(this).attr('href',prefix+currentHref);
});
使用jQuery
這樣的東西應該可以解決問題:
$('a').each(function() {
$(this).prop('href', 'http://prefix.com/?x=' + $(this).prop('href'))
});
此類url重寫應僅在Web服務器級別處理。
如果您的Web服務器是Apache,這是您必須在httpd.conf中執行的操作。
RewriteCond %{HTTP_HOST} !^www\.fpqr\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://www.xyz.com/url/%{HTTP_HOST} [L,R]
var prefix = 'prefix';
$('a').attr('href', prefix +$('a').attr('href'));
希望這會有所幫助。
有一個(活動的)鏈接集合,其中包含頁面中的所有鏈接,因此您可以輕松地訪問頁面中的所有鏈接並按如下方式修改其href:
var links = document.links;
for (var i=0, iLen=links.length; i<iLen; i++) {
// use of \/\/ is to avoid SO posting rules
links[i].href = 'http:\/\/xyz.com/url?' + links[i].href;
}
請注意,並非所有A元素都是鏈接,它們可以是錨點(即,具有名稱或ID且沒有href屬性)。 添加href會更改鏈接的錨點,也許不是您想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.