开发者

How to use Regular Expression to extract information from a HTML webpage?

How to use Regular Expression to extract the answer "Here is the answer" from a HTML webpage like this?

  <b>Last Question:</b>
  <b>Here is the开发者_如何学Python answer</b>


I know regex is not recommeded to parse html but to answer your question, if you are using php simplehtmldom is your friend. http://simplehtmldom.sourceforge.net/


Thanks everybody!

Here is my solution by using BeautifulSoup since I'm using Python framework:

  response = opener.open(url)
  the_page = response.read()

  soup = BeautifulSoup(''.join(the_page))
  paraText1 = soup.body.find('div', 'div_id', text = u'Last Question:')

  if paraText1:
    answer = paraText1.next


Don't use regexen to parse HTML. This goes double if instead of well-formed SGML/XML/HTML5 you have tag soup.


Don't use regex. Use a HTML parser like Jsoup.

String html = "<b>Last Question:</b><b>Here is the answer</b>";
Document document = Jsoup.parse(html);
Element secondBold = document.select("b").get(1);
System.out.println(secondBold.text()); // Here is the answer

Jsoup is Java based. For other programming languages there are also HTML parsers available. If you're using C#, have a look at Nsoup. If you're using PHP, have a look at phpQuery (all of those parsers use jQuery-like CSS3 selectors to select elements, which is simply brilliant).


As Charles said, don't use regex for this; if you're using PHP I'd recommend the DOM parsing functionality built in, coupled with the XPath methods proves quite reliable.

If you're more open than that I'd recommend using jQuery to do the job via Node.js, been doing that a lot lately myself- it makes life easy.


<b>Last Question:</b>\\s*(<b>.*?</b>)

Or, in more details,

String x  ="<b>Last Question:</b>\n<b>Here is the answer</b>";
Pattern p = Pattern.compile("<b>Last Question:</b>\\s*(<b>.*?</b>)");
Matcher m = p.matcher(x);
if (m.find())
   System.out.println(m.group(1));

Regular expressions are still an option when HTML or similar tags are just not present or appear at random without providing enough context information on their own. In such cases, we need to look into some words of the human speech instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜