Extract URLs from HTML code using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract URLs from HTML code using sed
# 8  
Old 11-29-2009
Hi.

You can use lynx to do most of the hard work:
Code:
#!/usr/bin/env bash

# @(#) s3	Demonstrate extraction of links with "lynx".

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) lynx 
set -o nounset
echo

URL="http://www.google.com/search?q=chondroitin&num=100&start=200"

echo " Looking at URL: $URL"

echo
echo " Results:"
lynx -dump -listonly -hiddenlinks=ignore "$URL" |
grep -i -v google |
sed -e 's/^[ 	]*[0-9.]*[ 	]*//' > t1

echo " Extracted about $( wc -l <t1 ) non-google links; first 10:"
head -10 t1

exit 0

producing:
Code:
% ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
Lynx Version 2.8.7dev.9 (27 Apr 2008)

 Looking at URL: http://www.google.com/search?q=chondroitin&num=100&start=200

 Results:
 Extracted about 157 non-google links; first 10:

References

http://www.wisegeek.com/what-is-chondroitin.htm
http://74.125.95.132/search?q=cache:ZwdfFnJUbecJ:www.wisegeek.com/what-is-chondroitin.htm+chondroitin&cd=201&hl=en&ct=clnk&gl=us&ie=UTF-8
http://shaokang2002.en.busytrade.com/products/info/24144/_Chondroitin_sulphate.html
http://74.125.95.132/search?q=cache:B32adgCixNAJ:shaokang2002.en.busytrade.com/products/info/24144/_Chondroitin_sulphate.html+chondroitin&cd=202&hl=en&ct=clnk&gl=us&ie=UTF-8
http://www.ebmonline.org/cgi/content/abstract/226/2/144
http://www.ebmonline.org/cgi/content/abstract/230/4/255
http://linkinghub.elsevier.com/retrieve/pii/S0255270106002182

Some clean-up is done by getting rid of google references, and removing the initial whitespace and integer on each line ... cheers, drl
# 9  
Old 11-29-2009
Try this awk solution
Code:
wget -q -U "Mozilla/5.001" -O - "http://www.google.com/search?q=searchphrase&num=100&start=200" | awk -F\" '{for(i=0;++i<=NF;){if($i ~ /^http/ && $i !~ "google\|74.125.95"){print $i}}}'


Last edited by danmero; 11-29-2009 at 12:24 PM.. Reason: Filter out google links
# 10  
Old 11-29-2009
gawk
Code:
wget -q -U "Mozilla/5.001" -O - "http://www.google.com/search?q=searchphrase&num=100&start=200" | awk -vRS="</a>" '
{
  gsub(/.*<a +href=\042/,"")
  gsub(/\042.*/,"")
  print 
}'

# 11  
Old 11-29-2009
Or:

Code:
wget -q -U "Mozilla/5.001" -O - 'http://www.google.com/search?q=searchphrase&num=100&start=200' | 
  perl -lne'
    print$1while/<\s*a\s+href\s*=\s*["\047]([^"\047]+)/ig
    '

# 12  
Old 11-30-2009
Hello, and thx at all for so much replies !

@ ghostdog74 and radoulov
i get all links(all what apears after href=" in code) by using yours solutions, but i need just search results.

@ danmero and drl
your scripts produce more usefull results, but google and google-cache urls must be keept out.

KenJackson's brings just 5 lines some code, sory, where are links ?Smilie

and Scrutinizer's solution ist the best presented here. Its simply and it works.
Code:
wget -q -U "Mozilla/5.001" -O - "http://www.google.com/search?q=searchphrase&num=100&start=200" | \
grep -o '<a href="http[^"]*"'|grep -v 'search?q=cache:'|grep -v '\.google\.'|sed 's/<a href="//;s/"$//'

Some filtering is needed but its ok.

anyone to make his solution more simplier ? Smilie


# 13  
Old 11-30-2009
Quote:
Originally Posted by L0rd
@ danmero and drl
your scripts produce more usefull results, but google and google-cache urls must be keept out.
Code:
# wget -q -U "Mozilla/5.001" -O - "http://www.google.com/search?q=searchphrase&num=100&start=200"|awk -F\" '{for(i=0;++i<=NF;){if($i ~ /^http/ && $i !~ "google\|cache:"){print $i}}}'

And the output is(first and last 10 records):
Code:
http://www.youtube.com/results?q=searchphrase&num=100&um=1&ie=UTF-8&sa=N&hl=en&tab=w1
http://www.christopherwardforum.com/viewtopic.php?f=3&t=372
https://www.unix.com/shell-programming-scripting/113921-getting-15-characters-after-search-phrase.html
http://www.zeromillion.com/srs-search-phrase
http://www.fwicki.com/fwickis/science/Humanities
http://www.articlealley.com/tags-9091.html
http://www.workz.com/content/view_content.html?section_id=466&content_id=6151
http://www.onlinemarketingelite.com/tag/search-phrase/
http://www.discoveres.com/search-phrase_SRS/
http://www.articlesfactory.com/search/Search%20Phrase/
......
http://www.learninghownow.com/blog/tag/search-phrase/
http://www.quakerranter.org/tag/search%20phrase
http://forums.digital-m.co.za/showthread.php?t=73
http://drupal.org/node/527084
http://www.smartertools.com/forums/ThreadNavigation.aspx?PostID=57990&NavType=Previous
http://www.tversoft.com/computer/search-phrase.html
http://www.hotfroguk.co.uk/Companies/Search-Phrase-Builder
http://search.infospace.com/ispace/ws/redir/qcat=News/qcoll=relevance/qkw=Enter%20A%20Search%20Phrase/rfcp=RightNav/rfcid=302364/_iceUrlFlag=11?_IceUrl=true
http://www.stumbleupon.com/stumbler/kaylavincent/tag/search-phrase/
http://en.drigger.com/e/1012902/obscure_search_phrase

# 14  
Old 12-01-2009
Quote:
Originally Posted by L0rd
KenJackson's brings just 5 lines some code, sory, where are links ?Smilie
I didn't give a complete solution, that's why I called it a starter and referenced the looping.

I am awed by the power of sed. I routinely use it's regular expression capability, but I rarely use hold buffer and looping command. It has been my goal for some time to become skilful at using these. Your question would have been the perfect opportunity for me to dig in and come up with a solution that demonstrates that power. But I flat did not have the time then, and it looks like you have a solution now that you find satisfying. I'll work on it off-line.

Quote:
Originally Posted by L0rd
and Scrutinizer's solution ist the best presented here. Its simply and it works.
Yeah, I've noticed Scrutinizer writes some good and straight-forward code. Stick with him.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk/sed HTML extract

I'm extracting text between table tags in HTML <th><a href="/wiki/Buick_LeSabre" title="Buick LeSabre">Buick LeSabre</a></th> using this: awk -F "</*th>" '/<\/*th>/ {print $2}' auto2 > auto3 then this (text between a href): sed -e 's/\(<*>\)//g' auto3 > auto4 How to shorten this into one... (8 Replies)
Discussion started by: p1ne
8 Replies

2. Shell Programming and Scripting

Trying to extract domain and tld from list of urls.

I have done a fair amount of searching the threads, but I have not been able to cobble together a solution to my challenge. What I am trying to do is to line edit a file that will leave behind only the domain and tld of a long list of urls. The list looks something like this: www.google.com... (3 Replies)
Discussion started by: chamb1
3 Replies

3. Shell Programming and Scripting

How to remove urls from html files

Does anybody know how to remove all urls from html files? all urls are links with anchor texts in the form of <a href="http://www.anydomain.com">ANCHOR</a> they may start with www or not. Goal is to delete all urls and keep the ANCHOR text and if possible to change tags around anchor to... (2 Replies)
Discussion started by: georgi58
2 Replies

4. Shell Programming and Scripting

help with sed needed to extract content from html tags

Hi I've searched for it for few hours now and i can't seem to find anything working like i want. I've got webpage, saved in file par with form like this: <html><body><form name='sendme' action='http://example.com/' method='POST'> <textarea name='1st'>abc123def678</textarea> <textarea... (9 Replies)
Discussion started by: seb001
9 Replies

5. Shell Programming and Scripting

Remove external urls from .html file

Hi everyone. I have an html file with lines like so: link href="localFolder/..."> link href="htp://..."> img src="localFolder/..."> img src="htp://..."> I want to remove the links with http in the href and imgs with http in its src. I'm having trouble removing them because there... (4 Replies)
Discussion started by: CowCow339
4 Replies

6. Shell Programming and Scripting

Extract urls from index.html downloaded using wget

Hi, I need to basically get a list of all the tarballs located at uri I am currently doing a wget on urito get the index.html page Now this index page contains the list of uris that I want to use in my bash script. can someone please guide me ,. I am new to Linux and shell scripting. ... (5 Replies)
Discussion started by: mnanavati
5 Replies

7. Shell Programming and Scripting

SED to extract HTML text data, not quite right!

I am attempting to extract weather data from the following website, but for the Victoria area only: Text Forecasts - Environment Canada I use this: sed -n "/Greater Victoria./,/Fraser Valley./p" But that phrasing does not sometimes get it all and think perhaps the website has more... (2 Replies)
Discussion started by: lagagnon
2 Replies

8. Shell Programming and Scripting

sed to extract only floating point numbers from HTML

Hi All, I'm trying to extract some floating point numbers from within some HTML code like this: <TR><TD class='awrc'>Parse CPU to Parse Elapsd %:</TD><TD ALIGN='right' class='awrc'> 64.50</TD><TD class='awrc'>% Non-Parse CPU:</TD><TD ALIGN='right' class='awrc'> ... (2 Replies)
Discussion started by: pondlife
2 Replies

9. UNIX for Advanced & Expert Users

sed to extract HTML content

Hiya, I am trying to extract a news article from a web page. The sed I have written brings back a lot of Javascript code and sometimes advertisments too. Can anyone please help with this one ??? I need to fix this sed so it picks up the article ONLY (don't worry about the title or date .. i got... (2 Replies)
Discussion started by: stargazerr
2 Replies

10. UNIX for Dummies Questions & Answers

How do I extract text only from html file without HTML tag

I have a html file called myfile. If I simply put "cat myfile.html" in UNIX, it shows all the html tags like <a href=r/26><img src="http://www>. But I want to extract only text part. Same problem happens in "type" command in MS-DOS. I know you can do it by opening it in Internet Explorer,... (4 Replies)
Discussion started by: los111
4 Replies
Login or Register to Ask a Question