-
-
Save sehe/c16725584493b021ba5b to your computer and use it in GitHub Desktop.
Using Boost.Spirit to extract certain tags/attributes from HTML http://stackoverflow.com/questions/8501935/using-boost-spirit-to-extract-certain-tags-attributes-from-html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <boost/xpressive/xpressive.hpp> | |
void handle_attr( | |
const std::string& elem, | |
const std::string& attr, | |
const std::string& value) | |
{ | |
if (elem == "img" && attr == "src") | |
std::cout << "value : " << value << std::endl; | |
} | |
typedef std::string::const_iterator It; | |
int main(int argc, const char *argv[]) | |
{ | |
using namespace boost::xpressive; | |
#if DYNAMIC | |
const sregex re = sregex::compile("<img\\s+[^\\>]*?src\\s*=\\s*([\"'])(.*?)\\1"); | |
#else | |
const sregex re = "<img" >> +_s >> -*(~(set = '\\','>')) >> | |
"src" >> *_s >> '=' >> *_s | |
>> (s1 = as_xpr('"') | '\'') >> (s2 = -*_) >> s1; | |
#endif | |
std::string s; | |
smatch what; | |
while (std::getline(std::cin, s)) | |
{ | |
It f = s.begin(), l = s.end(); | |
do | |
{ | |
if (!regex_search(f, l, what, re)) | |
break; | |
handle_attr("img", "src", what[2]); | |
f = what[0].second; | |
} while (f!=s.end()); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: spirit regex expressive | |
#CPPFLAGS+=-std=c++0x | |
CPPFLAGS+=-g -O3 | |
CPPFLAGS+=-I /home/sehe/custom/boost_1_48_0 | |
CPPFLAGS+=-L /home/sehe/custom/boost_1_48_0/stage/lib/ | |
LDFLAGS+=-lboost_regex | |
CPPFLAGS+=-Wall | |
%:%.cpp | |
g++ $(CPPFLAGS) $^ -o $@ $(LDFLAGS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <boost/regex.hpp> | |
void handle_attr( | |
const std::string& elem, | |
const std::string& attr, | |
const std::string& value) | |
{ | |
if (elem == "img" && attr == "src") | |
std::cout << "value : " << value << std::endl; | |
} | |
typedef std::string::const_iterator It; | |
int main(int argc, const char *argv[]) | |
{ | |
const boost::regex re("<img\\s+[^\\>]*?src\\s*=\\s*([\"'])(.*?)\\1"); | |
std::string s; | |
boost::smatch what; | |
while (std::getline(std::cin, s)) | |
{ | |
It f = s.begin(), l = s.end(); | |
do | |
{ | |
if (!boost::regex_search(f, l, what, re)) | |
break; | |
handle_attr("img", "src", what[2]); | |
f = what[0].second; | |
} while (f!=s.end()); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#define BOOST_SPIRIT_DEBUG | |
#include <string> | |
#include <iostream> | |
#include <boost/spirit/include/qi.hpp> | |
#include <boost/spirit/include/phoenix.hpp> | |
namespace qi = boost::spirit::qi; | |
namespace phx = boost::phoenix; | |
void handle_attr( | |
const std::string& elem, | |
const std::string& attr, | |
const std::string& value) | |
{ | |
if (elem == "img" && attr == "src") | |
std::cout << "value : " << value << std::endl; | |
} | |
typedef std::string::const_iterator It; | |
typedef qi::space_type Skipper; | |
struct grammar : qi::grammar<It, Skipper> | |
{ | |
grammar() : grammar::base_type(html) | |
{ | |
using namespace boost::spirit::qi; | |
using phx::bind; | |
attr = as_string [ +~char_("= \t\r\n/>") ] [ _a = _1 ] | |
>> '=' >> ( | |
as_string [ '"' >> lexeme [ *~char_('"') ] >> '"' ] | |
| as_string [ "'" >> lexeme [ *~char_("'") ] >> "'" ] | |
) [ boost::phoenix::bind(handle_attr, _r1, _a, _1) ] | |
; | |
elem = lit('<') | |
>> as_string [ lexeme [ ~char_("-/>") >> *(char_ - space - char_("/>")) ] ] [ _a = _1 ] | |
>> *attr(_a); | |
html = (-elem) % +("</" | (char_ - '<')); | |
BOOST_SPIRIT_DEBUG_NODE(html); | |
BOOST_SPIRIT_DEBUG_NODE(elem); | |
BOOST_SPIRIT_DEBUG_NODE(attr); | |
} | |
qi::rule<It, Skipper> html; | |
qi::rule<It, Skipper, qi::locals<std::string> > elem; | |
qi::rule<It, qi::unused_type(std::string), Skipper, qi::locals<std::string> > attr; | |
}; | |
int main(int argc, const char *argv[]) | |
{ | |
std::string s; | |
const static grammar html_; | |
while (std::getline(std::cin, s)) | |
{ | |
It f = s.begin(), | |
l = s.end(); | |
if (!phrase_parse(f, l, html_, qi::space) || (f!=l)) | |
std::cerr << "unparsed: " << std::string(f,l) << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment