Extract full link of a div element

I'm downloading episodes from a series, but I'm tired for joining in the site, copying the link and downloading it for each episode.

First, I join in the site and I get the HTML code. The code is big, but I'm interested in this in particular (it is where the link is)

 var e=Array(),d=1;e[1]='<div><object width="720" height="450" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=""><param name="movie" value="//"><param name="bgcolor" value="#FFFFFF"><param name="allowfullscreen" value="true"><param name="flashvars" value="flvurl="><embed src="//" type="application/x-shockwave-flash"width="720" height="450" bgcolor="#FFFFFF"flashvars="flvurl="allowFullScreen="true" /></object></div>';e[2]='<div><iframe src="" width="720" height="450" scrolling="no" frameborder="0"></iframe></div>';

Well, its a looong code, but I'm interested to get this. (There are 2 identical links, both work without the '&amp' in the end)

Now, I'm using this regEX (Note I'm noob with this, I test it and works)

\s*flvurl\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));*%26key%3Dck2&amp

And, in the terminal I use

wget -q -O - "serverlink" | sed -e "s/\s*flvurl\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));*%26key%3Dck2&amp//g"

I get error with unexpected token) '`

Does anyone know how I can fix it? (Or, maybe, someone knows a better regex to get the link from the first code box)

10

2 Answers

Here are some simpler approaches to get what you want:

  1. grep with PCREs

    wget -q -O - "serverlink" | grep -oP 'flvurl=\K[^;]+(?=&amp;)' 

    Explanation

    In PCREs, the \K means "discard everything matched until here". The -o option to grep means "print only the matched portion of a string". So, grep -oP 'flvurl=\K means: "look for flvurl= and start matching after it." The [^;]+ means "match as many non-; characters as possible.

    The foo(?=bar) format is called a positive lookahead. It is not part of the actual matching string. It means _"match foo followed by bar but discard the bar. Putting all this together, the regular expression above will print everything between flvurl= and &amp;.

  2. sed

    wget -q -O - "serverlink" | sed -nr 's/.*flvurl=([^;]+)&amp;.*/\1/p' 

    Explanation

    The substitution operator (I use the traditional s/// instead of the s\\\ you used but its the same idea), will substitute everything with the text between flvurl= and &amp;. The parentheses are there to "capture" the matched string, making it available as \1. The -r option enables extended regular expressions which can deal with parentheses without needing to escape (\( and \)) them. That's the reason you were getting the error by the way.

    The -n suppresses normal output, no lines will be printed by default. The p at the end of the substitution (s///p) means "print the current line if the substitution was successful".

  3. Perl

    Using the same logic as the sed above:

    wget -q -O - "serverlink" | perl -ne 's/.*flvurl=([^;]+)&amp;.*/$1/ && print' 
3

Your command line is piping the output of wget into sed - in order to substitute the result of the sed command as a URL argument on the wget command line, you will need to do something like

wget -q -O- -- $(echo -n "urlstring" | sed -e 's/pattern/replacement/')

or

wget -q -O- -- $(sed -e 's/pattern/replacement/' <<< "urlstring")

The -e may be omitted in this context.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like