Sponsored Content
Top Forums Shell Programming and Scripting Extract/Parse information from html (website) Post 302627647 by birei on Saturday 21st of April 2012 05:00:16 AM
Old 04-21-2012
One way:
Code:
$ cat script.pl 
use warnings;
use strict;
use WWW::Mechanize;
use HTML::TokeParser;
use HTML::Entities;

my $uri = q[http://www.energiecontracting.de/7-mitglieder/von-A-Z.php?a_z=B&seite=1];

## Get the agent to explore the web page.
my $mech = WWW::Mechanize->new();
$mech->agent_alias( q[Linux Mozilla] );
$mech->get( $uri );

## Get last page.
my @c = $mech->find_all_links(
                q[url_regex] => qr/(?i:seite)=/,
);
my %d = map { $_->[0] => do { $_->[0] =~ m/(\d+)\Z/; $1 } } @c;
my $last_page = (sort { $b <=> $a } values %d)[0];

my @text;
for my $page ( 1 .. $last_page ) {

        my $tp = HTML::TokeParser->new( \$mech->content() ) or die qq[ERROR in HTML::TokeParser\n];
        $tp->get_tag( q[table] );

        while ( 1 ) {
                my $t = $tp->get_text();
                if ( $t ) {
                        last if $t =~ m/\A(?i)seite/;
                        push @text, $t;
                }
                my $token = $tp->get_token;
                if ( $token->[0] eq q[E] && $token->[1] eq q[p] ) {
                        printf qq[%s\n], join q[,], @text;
                        @text = ();
                        next;
                }
                if ( $token->[0] eq q[E] && $token->[1] eq q[div] ) {
                        last;
                }
        }

        $uri =~ s/(\d+)\Z/$1 + 1/e;
        $mech->get( $uri );
}

exit 0;
$ perl script.pl
Siegeltr�ger,Badische Kraftwerk GmbH & Co. KG,76532 Baden-Baden
Contractor,Bayerische Elektrizit�tswerke GmbH,86150 Augsburg,Tel.: +49 (0821) 328 - 0,Fax: +49 (0821) 328 - 4160,undine.maidl@lew.de,www.bew-augsburg.de
Siegeltr�ger,BayWa Energie Dienstleistungs GmbH,81925 M�nchen,Projekte dieser Firma ansehen
Siegeltr�ger,BEG Energiegesellschaft mbH,12681 Berlin
Partnerunternehmen,Beratungs- und Planungsb�ro f�r MULTIVALENTE Beheizungssysteme,Dipl.-Ing. G�nter Schlagowski,28213 Bremen,Tel.: +49 (0421) 211210,Fax: +49 (0421) 212772,g.s.nestwaerme@t-online.de,www.schlagowski.de,Weitere Informationen
Interessent,Bernd Wiggenhauser,78234 Engen
Interessent,Berndorff Contracting GmbH,50674 K�ln
Contractor,beta GmbH Betrieb energietechnischer Anlagen,30451 Hannover,Tel.: +49 (0511) 45001109,Fax: +49 (0511) 497574,brosziewski@beta-energie.de,www.beta-energie.de
Siegeltr�ger,BEVR Biomasse Energie Versorgung Ratekau GmbH & Co. KG,23684 Schulendorf
Siegeltr�ger,BHK-Systeme GmbH,10243 Berlin
Interessent,Bi. En GmbH & Co. KG,24109 Kiel
Siegeltr�ger,BIBER Biomasse GmbH,94333 Geiselh�ring,Projekte dieser Firma ansehen
Siegeltr�ger,Bio W�rme Rh�n GmbH & Co. KG,36145 Hofbieber-Obern�st,Projekte dieser Firma ansehen
Siegeltr�ger,Bio-W�rme-Innovation GmbH,06449 Aschersleben
Interessent,Bioenergie-Regional GmbH,74199 Untergruppenbach
Siegeltr�ger,Bioenergiehof B�hme GmbH,01762 Obercarsdorf
Siegeltr�ger,Bisser-Putz re-Solution Energietechnik GbR,78606 Seitingen-Oberflacht
Siegeltr�ger,Blume W�rmelieferungs GmbH,14728 Rhinow
Siegeltr�ger,Bosch Energy and Building Solutions GmbH,70499 Stuttgart
Partnerunternehmen,Bosch Thermotechnik GmbH Buderus Deutschland,Dipl.-Ing. Jens Gierok,21035 Hamburg,Tel.: +49 (040) 73417 - 0,Fax: +49 (040) 73417 - 267,jens.gierok@buderus.de,www.buderus.de,Weitere Informationen
Partnerunternehmen,BRANDES GmbH,Karin Brandes,23701 Eutin,Tel.: +49 (04521) 807 - 0,Fax: +49 (04521) 807 - 77,karin.brandes@brandes.de,www.brandes.de,Weitere Informationen
Contractor,BRASST Energiedienstleistungen GmbH,13088 Berlin,Tel.: +49 (030) 556885 - 0,Fax: +49 (030) 556885 - 99,brasst@bln.de,www.brasst.de
Contractor,BTB  Blockheizkraftwerks- Tr�ger- und Betreiberges. mbH Berlin,10589 Berlin,Tel.: +49 (030) 349907 - 61,Fax: +49 (030) 349907 - 88,karl.meyer@btb-berlin.de,www.btb-berlin.de,Projekte dieser Firma ansehen

 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Using Perl to query a website and parse the result

Hi, I am a JAVA programmer and I have no idea about perl. I did use it a long time ago and I don't even remember the basics. So here is my problem: In my work, I am supposed to build a simple program that opens a website (Gene Ontology)and passes my query and returns the result into a file. The... (1 Reply)
Discussion started by: chavanak
1 Replies

3. Shell Programming and Scripting

Trying to Parse Version Information from Text File

I have a file name version.properties with the following data: major.version=14 minor.version=234 I'm trying to write a grep expression to only put "14" to stdout. The following is not working. grep "major.version=(+)" version.properties What am I doing wrong? (6 Replies)
Discussion started by: obfunkhouser
6 Replies

4. Shell Programming and Scripting

sed to parse html

Hello, I have a html file like this : <html> ... ... ... <table> ....... ...... </table> <table name = "hi"> ...... ..... ... </table> <h1> Welcome </h1> ....... ...... </html> (11 Replies)
Discussion started by: prasanna1157
11 Replies

5. Shell Programming and Scripting

feasibility of opening a website link from unix and get a response in the form of xml or html

i just wanted to know whether is it possible to open a website link and get a response in the form of xml or html format... the website is of local network... for example something like this wget http://blahblah.samplesite.com/blachblahcblach/User/jsp/ShowPerson.jsp?empid=123456 ... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Parse excel file with html on each cell

<DIV><P>Pré-condição aceder ao ecrã Home do MRS.</P></DIV><DIV><P>OK.</P></DIV><DIV><P>Seleccionar Pesquisa de Recepção Directa.</P></DIV><DIV><P>Confirmar que abriu ecrã de Recepção Directa.</P></DIV><DIV> (6 Replies)
Discussion started by: oliveiraum
6 Replies

7. Shell Programming and Scripting

awk to parse html file

Is it possible in awk to parse a webpage (EDAR Gene Sequencing - Genetic Testing Company | The DNA Diagnostic Experts | GeneDx), the source code is attached. <title> EDAR Gene Sequencing <dt>Test Code:</dt> <dd>156 </dd> <dt>Turnaround Time:</dt> <dd>6-8 weeks </dd> ... (4 Replies)
Discussion started by: cmccabe
4 Replies

8. Shell Programming and Scripting

Parse multiple html files in directory

I have downloaded source code for 97 files using: wget -x -i link.txt then run a rename loop: for file in * do mv $file $file.txt done to keep the html tags but make the file a text that can be parsed. In each of the 97 txt files the gene # is variable, but the gene is associated... (15 Replies)
Discussion started by: cmccabe
15 Replies

9. Shell Programming and Scripting

Parse html

I downloaded source code using: wget -qO- http://fulgentdiagnostics.com/test/clinical-exome/ | cat > flugentsource.txt Now I am trying to use sed to parse it to confirm a gene count. Basically, output (flugent.txt) all the gene names with a total count after them I'm not all that... (5 Replies)
Discussion started by: cmccabe
5 Replies

10. UNIX for Beginners Questions & Answers

How to parse a specifc value between html tags using sed?

Hi, im trying to read a Temperature value from html code. So far i have managed to reduce the whole html page down to this single line with the following sed command:sed -n '/Temperature/p' $temp_temperature | tee temp_string <TD width='350'>Temperature :</td><td>25... (2 Replies)
Discussion started by: naittis
2 Replies
HTML::TokeParser(3)					User Contributed Perl Documentation				       HTML::TokeParser(3)

NAME
HTML::TokeParser - Alternative HTML::Parser interface SYNOPSIS
require HTML::TokeParser; $p = HTML::TokeParser->new("index.html") || die "Can't open: $!"; while (my $token = $p->get_token) { #... } DESCRIPTION
The "HTML::TokeParser" is an alternative interface to the "HTML::Parser" class. It is an "HTML::PullParser" subclass. The following methods are available: $p = HTML::TokeParser->new( $file_or_doc ); The object constructor argument is either a file name, a file handle object, or the complete document to be parsed. If the argument is a plain scalar, then it is taken as the name of a file to be opened and parsed. If the file can't be opened for reading, then the constructor will return an undefined value and $! will tell you why it failed. If the argument is a reference to a plain scalar, then this scalar is taken to be the literal document to parse. The value of this scalar should not be changed before all tokens have been extracted. Otherwise the argument is taken to be some object that the "HTML::TokeParser" can read() from when it needs more data. Typically it will be a filehandle of some kind. The stream will be read() until EOF, but not closed. $p->get_token This method will return the next token found in the HTML document, or "undef" at the end of the document. The token is returned as an array reference. The first element of the array will be a (mostly) single character string denoting the type of this token: "S" for start tag, "E" for end tag, "T" for text, "C" for comment, "D" for declaration, and "PI" for process instructions. The rest of the array is the same as the arguments passed to the corresponding HTML::Parser v2 compatible callbacks (see HTML::Parser). In summary, returned tokens look like this: ["S", $tag, $attr, $attrseq, $text] ["E", $tag, $text] ["T", $text, $is_data] ["C", $text] ["D", $text] ["PI", $token0, $text] where $attr is a hash reference, $attrseq is an array reference and the rest is plain scalars. $p->unget_token($token,...) If you find out you have read too many tokens you can push them back, so that they are returned the next time $p->get_token is called. $p->get_tag( [$tag, ...] ) This method returns the next start or end tag (skipping any other tokens), or "undef" if there are no more tags in the document. If one or more arguments are given, then we skip tokens until one of the specified tag types is found. For example: $p->get_tag("font", "/font"); will find the next start or end tag for a font-element. The tag information is returned as an array reference in the same form as for $p->get_token above, but the type code (first element) is missing. A start tag will be returned like this: [$tag, $attr, $attrseq, $text] The tagname of end tags are prefixed with "/", i.e. end tag is returned like this: ["/$tag", $text] $p->get_text( [$endtag] ) This method returns all text found at the current position. It will return a zero length string if the next token is not text. The optional $endtag argument specifies that any text occurring before the given tag is to be returned. Any entities will be converted to their corresponding character. The $p->{textify} attribute is a hash that defines how certain tags can be treated as text. If the name of a start tag matches a key in this hash then this tag is converted to text. The hash value is used to specify which tag attribute to obtain the text from. If this tag attribute is missing, then the upper case name of the tag enclosed in brackets is returned, e.g. "[IMG]". The hash value can also be a subroutine reference. In this case the routine is called with the start tag token content as its argument and the return value is treated as the text. The default $p->{textify} value is: {img => "alt", applet => "alt"} This means that <IMG> and <APPLET> tags are treated as text, and that the text to substitute can be found in the ALT attribute. $p->get_trimmed_text( [$endtag] ) Same as $p->get_text above, but will collapse any sequences of white space to a single space character. Leading and trailing white space is removed. EXAMPLES
This example extracts all links from a document. It will print one line for each link, containing the URL and the textual description between the <A>...</A> tags: use HTML::TokeParser; $p = HTML::TokeParser->new(shift||"index.html"); while (my $token = $p->get_tag("a")) { my $url = $token->[1]{href} || "-"; my $text = $p->get_trimmed_text("/a"); print "$url $text "; } This example extract the <TITLE> from the document: use HTML::TokeParser; $p = HTML::TokeParser->new(shift||"index.html"); if ($p->get_tag("title")) { my $title = $p->get_trimmed_text; print "Title: $title "; } SEE ALSO
HTML::PullParser, HTML::Parser COPYRIGHT
Copyright 1998-2001 Gisle Aas. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.8.0 2001-04-10 HTML::TokeParser(3)
All times are GMT -4. The time now is 01:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy