Solved

Updating links on a webpage?

Posted on 2001-07-17
26
243 Views
Last Modified: 2013-12-25
HI i need a small program that i can run on my server, it should be able to update some links on a .html page.
I dont know programming, so i dont know how difficoult this is..but i would like to get a easy way to update several links on a static webpage...

the program should be able to choose 1 of the files in a folder fx. sport.html  and then change the location of the defined links on this page..

sport.html contains fx 10links that i want to update...I have these links in a txt file, but i think it would be easiest if i could copy the url lines from the txt file into a area of the program, so it will update them one by one..

sport.html should maybe have a template version called sport2.html that is the one that is being updated and saved to the originally file?

please just ask, i dont know if i explained it corectly...


thanks

nick




0
Comment
Question by:nicce
26 Comments
 

Author Comment

by:nicce
Comment Utility
this is how the sport page could look:
and its only this part that is going to be updated:
http://www.url_1_from_txtfile.html


<html>

<head>
<title>sport pictures</title>
</head>
<body>
<p>&nbsp;</p>
<p align="center">sport pictures</p>
<p align="center">Golf!!</p>
<p align="center"><a href="http://www.other_link_not_updated.html">other link</a></p>
<p>&nbsp;</p>

<p>&nbsp;New <a href="http://www.url_1_from_txtfile.html(line 1 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_2_from_txtfile.html(line 2 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_3_from_txtfile.html(line 3 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_4_from_txtfile.html(line 4 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_5_from_txtfile.html(line 5 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_6_from_txtfile.html(line 6 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_7_from_txtfile.html(line 7 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_8_from_txtfile.html(line 8 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_9_from_txtfile.html(line 9 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_10_from_txtfile.html(line 10 from txt file)">golf pictures</a></p>

</body>
</html>
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
What OS?  Would a perl solution be acceptable?
0
 
LVL 3

Accepted Solution

by:
interiot earned 300 total points
Comment Utility
Here's a perl solution.  I'll be happy to explain how to set it up, if you'll let me know what OS you're running.



#! /usr/bin/perl -w

# What the lines to change start with
my $line_change_start = '<p>&nbsp;New ';

if (@ARGV < 2) {
        print "syntax:  $0 <html file>  <url list file>\n";
        exit(1);
}

my ($html_file, $url_file) = @ARGV;

open HTML, "<$html_file"        or die "$_";
@html_contents = <HTML>;
close HTML;

open URLS, "<$url_file"         or die "$_";
for ($ctr=0; $ctr<@html_contents; $ctr++) {
        $_ = $html_contents[$ctr];
        if (substr($_, 0, length($line_change_start)) eq $line_change_start) {
                my $url = <URLS>;   chomp $url;
                s/href=\s*[^>\s]*/href="$url"/;
                $html_contents[$ctr] = $_;
        }
}
close URLS;

open HTML, ">$html_file"        or die "$_";
print HTML @html_contents;
close HTML;



Run it like this:
     link_updater.pl webpage.html url_list.txt

You may want to change $line_change_start to whatever the lines in the template begin with that you want changed.
0
 

Author Comment

by:nicce
Comment Utility
hi my server uses Linux 2.2.18
0
 

Author Comment

by:nicce
Comment Utility
interiot all solutions are acceptable :) it just needs to work:)

i dont know how your perl script works but it looks simple:) which can be both good bad..hehe

the output html could also look like this so the links needs to be associated with a line in the txt file i think?:
<p>&nbsp;New <a href="http://www.url_1_from_txtfile.html(line 1 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_2_from_txtfile.html(line 2 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_3_from_txtfile.html(line 3 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_4_from_txtfile.html(line 4 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_5_from_txtfile.html(line 5 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.notupdated.html(should not be updated)">mainpage</a></p>
<p>&nbsp;New <a href="http://www.url_6_from_txtfile.html(line 6 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_7_from_txtfile.html(line 7 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_8_from_txtfile.html(line 8 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_9_from_txtfile.html(line 9 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_10_from_txtfile.html(line 10 from txt file)">golf pictures</a></p>



0
 

Author Comment

by:nicce
Comment Utility
interiot all solutions are acceptable :) it just needs to work:)

i dont know how your perl script works but it looks simple:) which can be both good bad..hehe

the output html could also look like this so the links needs to be associated with a line in the txt file i think?:
<p>&nbsp;New <a href="http://www.url_1_from_txtfile.html(line 1 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_2_from_txtfile.html(line 2 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_3_from_txtfile.html(line 3 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_4_from_txtfile.html(line 4 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_5_from_txtfile.html(line 5 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.notupdated.html(should not be updated)">mainpage</a></p>
<p>&nbsp;New <a href="http://www.url_6_from_txtfile.html(line 6 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_7_from_txtfile.html(line 7 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_8_from_txtfile.html(line 8 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_9_from_txtfile.html(line 9 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_10_from_txtfile.html(line 10 from txt file)">golf pictures</a></p>



0
 

Author Comment

by:nicce
Comment Utility
the links in the template file should maybe look like this:
<a href="#%URL1%#">golf pictures</a></p>
<a href="#%URL2%#">golf pictures</a></p>
<a href="#%URL3%#">golf pictures</a></p>

<a href="http://www.mainpage.com">Back to mainpage!</a></p>

<a href="#%URL4%#">golf pictures</a></p>
<a href="#%URL6%#">golf pictures</a></p>
<a href="#%URL7%#">golf pictures</a></p>

<a href="http://www.yahoo.com">Visit Yahoo.com!</a></p>

ect....
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
Ahh, that's a good idea.  I changed it to use your template idea, and I commented it a bit.



#! /usr/bin/perl -w

if (@ARGV < 2) {
        print "syntax:  $0 <html file>  <url list file>\n";
        exit(1);
}

# Get the command line arguments
my ($html_file, $url_file) = @ARGV;

# Read in the entire html file
open HTML, "<$html_file"        or die "$_";
@html_contents = <HTML>;
close HTML;

# Read in the entire URL file
open URLS, "<$url_file"         or die "$_";
@urls = map {chomp;$_} <URLS>;
close URLS;

# Go through the html file, line by line.
for ($ctr=0; $ctr<@html_contents; $ctr++) {
        # Replace #%URL3%# with the contents of $urls[3]
        $html_contents[$ctr] =~ s/#\%URL(\d)\%#/$urls[$1]/ge;
}

# Write the contents of the html file back out
open HTML, ">$html_file"        or die "$_";
print HTML @html_contents;
close HTML;
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
To get the script to work, cut and paste the code to, say, link_update.pl.  Then run "chmod +x link_update.pl".  Then you should be able to simply type the
     link_updater.pl template.html url_list.txt

When it's done, template.html will be changed.  Or perhaps you want there to be an input template file and an outputted html file?
0
 

Author Comment

by:nicce
Comment Utility
Hi sorry i dont know what chmod +x link_update.pl means
im not doing the tech on my server.:)
maybe you can explain it a little moe how i uses this...
i dont have much more experience than usinf ftp and setting file permissions..!
0
 

Author Comment

by:nicce
Comment Utility
it would be nice with a output html file, so i can check if its ok and then i can rename it myself afterwards:)
0
 

Author Comment

by:nicce
Comment Utility
and another thing there will be more than 1 template..1 for each topic...so it would be cool with a interface where i can choose the template, and maybe a output file name?
maybe this to much to ask, but i dont know how hard this is, but sofar thank you very much:)
0
 

Author Comment

by:nicce
Comment Utility
and another thing there will be more than 1 template..1 for each topic...so it would be cool with a interface where i can choose the template, and maybe a output file name?
maybe this to much to ask, but i dont know how hard this is, but sofar thank you very much:)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:nicce
Comment Utility
and another thing there will be more than 1 template..1 for each topic...so it would be cool with a interface where i can choose the template, and maybe a output file name?
maybe this to much to ask, but i dont know how hard this is, but sofar thank you very much:)
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
Okay.  

link_update.pl:
---------------------------------------------------------------
#! /usr/bin/perl -w

if (@ARGV < 3) {
        print "syntax:  $0 <template filename>  <url list filename> <output filename>\n";
        exit(1);
}

# Get the command line arguments
my ($template_file, $url_file, $output_file) = @ARGV;

die "template file '$template_file' doesn't exist" if (! -e $template_file);
die "url list file '$url_file' doesn't exist"      if (! -e $url_file);

# Read in the URL list
open URLS, "<$url_file"         or die "$_";
@urls = map {chomp;$_} <URLS>;
close URLS;

# Read in the template, write to the output file
open TEMPLATE, "<$template_file"        or die "$_";
open OUTPUT, ">$output_file"            or die "$_";
while (<TEMPLATE>) {
        # Replace #%URL3%# with the contents of $urls[3]
        s/#\%URL(\d)\%#/$urls[$1]/ge;

        print OUTPUT $_;
}
close OUTPUT;
close TEMPLATE;



menu:
---------------------------------------------------------------
#! /bin/sh

dialog \
        --menu "Select which page to update the links for\\n" \
        23 80 4 \
        index   'Update homepage'               \
        page1   'Update the first subpage'      \
        page2   'Update the second subpage'     \
        page3   'Update the third subpage'      \
        2>/tmp/link_update_menu.out     || exit

filename=`cat /tmp/link_update_menu.out`
test -z "$filename" && exit

link_update.pl templates/$filename.html urls/$filename.urllist out/$filename.html

echo "$filename.html updated".

---------------------------------------------------------------


You'll want to modify the menu script quite a bit.  The 4  in "23 80 4" is the number of menu options that follow.  Then there's a list of files and descriptions of the files to pick from.

As it's set up, if you select 'index', it will use the template from templates/index.html, the URL list from urls/index.urllist, and output the results to out/index.html.
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
Oops.  The last two lines of the menu script should be:

link_update.pl templates/$filename.html urls/$filename.urllist out/$filename.html \
        && echo "$filename.html updated".
0
 

Author Comment

by:nicce
Comment Utility
great let me try to set it up:)..
do you have icq? it would be easier if i need some support:)?
0
 

Author Comment

by:nicce
Comment Utility
should i just upload it  and execute link_update.pl thru my browsor or?

it just shows the code in the window..??
0
 

Author Comment

by:nicce
Comment Utility
should i just upload it  and execute link_update.pl thru my browsor or?

it just shows the code in the window..??
0
 

Author Comment

by:nicce
Comment Utility
sorry for the double posts, i dont know why they appear..weird!!
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
No, I don't have ICQ set up at work.

I errored and wrote the program for the command line.  If you run it without modification, you'll have to just run 'menu' from the command line.
0
 

Author Comment

by:nicce
Comment Utility
sorry, what do you mean by command line? in dos or what?
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
Unix has a command line, similar to dos, but different.
0
 

Author Comment

by:nicce
Comment Utility
oh ok..i dont have access to anything on the server:(
what im interested in is a bowsor based update utillity:)
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Please update/finalize this question .... no activity since 7/18/01.  Also, you may benefit by checking the All Topics area to see if another topic area may fit.

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Comment from expert accepted as answer.

Thank you
Computer101
E-E Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now