开发者

HTTParty to parse HTML

开发者 https://www.devze.com 2023-02-15 03:26 出处:网络
I\'m looking for a way to pull specific content off of a web site that is fairly well formed, but not quite perfect xml:

I'm looking for a way to pull specific content off of a web site that is fairly well formed, but not quite perfect xml:

<html>
  <head>
    <title>title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <META HTTP-EQUIV="expires" CONTENT="now">
    <meta http-equiv=refresh conten开发者_JS百科t=300>
  </head>
<body bgcolor="#FFFFFF">
<p><font face="Arial, Helvetica, sans-serif" size="2"><img src="pict.gif" width="503" height="43"><br></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2">Please Note: ...<br></font></p>
<font face="Arial, Helvetica, sans-serif" size="3"><B>The Schedule</B></font><p></p>
<table border=0 width="100%">
  <tr> 
    <td><font face="Arial, Helvetica, sans-serif" size="2"><B>CONTENT A</B></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        <td><font face="Arial, Helvetica, sans-serif" size="2"><B>CONTENT B</B></font>&nbsp;&nbsp;</td>
        <td><font face="Arial, Helvetica, sans-serif" size="2"><B>CONTENT C</B></font>&nbsp;&nbsp;</td>
        <td><font face="Arial, Helvetica, sans-serif" size="2"><B>CONTENT D</B></font>&nbsp;&nbsp;</td>
    <td><font face="Arial, Helvetica, sans-serif" size="2"><B>CONTENT E</B></font></td>
  </tr>
...

Note the unterminated br's.

I'm trying to use HTTParty, and would like to do something like this:

include HTTParty
base_uri "http://website.com/"
basic_auth "name", "pw"
format :xml

def download_and_process_index_file  
  s = self.class.get("theurl.html")
  thehtml = s.parsed_response
  #print CONTENT A
  puts thehtml['html']['body']['table']['tr']['td']['font']['b']
end

But the xml won't parse, and if I switch to format :html then I don't seem to get any of the parsing goodness. Am I even close in my thinking here?

Thanks, Peter

0

精彩评论

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