开发者

How can I scrape/parse this data using regular expressions?

I'm really a beginner when it comes to regular expressions, and I'm not really sure where to start. I have some html code scraped from a web page and stored in a variable, and it looks something like this:

<thead><tr>
<th></th>
<th>GENERAL INFORMATION</th>
<th></th>
<th>DETAILED DAT开发者_运维问答A</th>
</tr></thead>
<tbody><tr>
<th>ID</th>
<td>123456789ABCD</td>
<th>Field1</th>
<td>6 = (Some-Specification (3 or more details))</td>

</tr></tbody>
<tbody><tr>
<th>AGL</th>
<td>1 - United States ; TH - Some Data</td>
<th>Field2</th>
<td>7 = (Option/Other Option)</td>
</tr></tbody>
<tbody><tr>
<th>MANUFACTURER</th>
<td>2010 SPECIFICATION  (ADSD: HMKC)</td>
<th>Field3</th>

<td>8 = (My Type)</td>
</tr></tbody>
<tbody><tr>
<th>MODEL</th>
<td>6X4 MY-MODEL/SOME_SPECS LONG SPECIFICATION, BLAH</td>
<th>Field4</th>
<td>9 = (STUFF/OTHER STUFF)</td>
</tr></tbody>
<tbody>

And then there is more of the same... I would like to parse the data from these cells into variables. (e.g. parse "123456789ABCD" into an ID variable) I'm working in ColdFusion and was thinking of using methods like REFindNoCase, REReplaceNoCase, SpanExcluding... Any idea how I can accomplish this? Or if you're not familiar with ColdFusion, even just the regular expressions necessary to parse this data would be very useful.


Don't use Regex for HTML. It will destroy you.

If you are doing a lot of this you should get an HTML tool such as TagSoup which normalizes the HTML. If you are working with web pages from one site, then you can create an XSLT stylesheet (or a DOM tool using XPath) which extracts the cells you want.

An Xpath for your cell (I have omitted the HTML namespace) could be

//tbody/tr[1]/td[1]

or you may wish to find rows by ID

//tbody/tr[th='ID']]/td

[The HTML looks rather messy - it uses th and td in the same tr which is not idiomatic.]


I agree with the main opinion on this platform that parsing HTML with regexes is not the "golden path". But in some cases it is just the easiest way to go and it just does what it needs to do.

This regex should do what you need:

<th>((?!</th>).)*</th>\s*<td>((?!</td>).)*</td>

Use the capturing group 1 for the key and group 2 for the value.

I dont know ColdFusion so I can not tell you how to apply it.


Use the CF xml parser, XmlParse. Looks like its based on strict XML though so make sure you run the input through something like htmltidy.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜