我应该使用正则表达式来解析这串html表数据吗?

发布于 2024-09-24 13:32:04 字数 2554 浏览 1 评论 0原文

解析这些数据的最佳方法是什么?我应该使用正则表达式还是其他东西?数据是 html 格式,但我从网站上找到了它,并将解析这个字符串,并且只解析这个字符串(注意:字符串更长 - 超过 1,300 个实例 - 下面只有两个) - 注意我使用 php & jquery 用于大多数 Web 编程。

我只需要提取第二个 td 中的数据以及锚点内的锚文本 - 在实例 1 中,它是:Best, Jahvid DET RB

我只需要运行此循环一次。

<tr class="oddtablerow"><td class="rank">1.</td><td class="rank">1.</td><td class="player"><a href="http://football22.myfantasyleague.com/2010/player?L=34793&amp;P=9839"  title="Salary: $2250000, Year: 3, Status: 3, Info: Drafted 10 1:04 Team, Week 3: at Vikings Sun 1:00 p.m. ET" class="position_rb">Best, Jahvid DET RB</a> (R) </td><td class="points tot">53.90</td><td class="points avg">26.950</td><td class="points"><a href="detailed?L=34793&amp;W=1&amp;P=9839&amp;YEAR=2010">17.55</a></td> 
<td class="points"><a href="detailed?L=34793&amp;W=2&amp;P=9839&amp;YEAR=2010">36.35</a></td> 
<td class="status"><a title="Owner: William Gold"  class="franchise_0009" href="http://football22.myfantasyleague.com/2010/options?L=34793&amp;F=0009&amp;O=01">Team Name</a> - <a href="options?L=34793&amp;O=05&amp;FRANCHISE=0013,0009&amp;PLAYER=9839,">Trade</a></td><td class="week">7</td><td class="salary">$2250000</td></tr> 
<tr class="eventablerow myfranchise "><td class="rank">2.</td><td class="rank">2.</td><td class="player"><a href="http://football22.myfantasyleague.com/2010/player?L=34793&amp;P=3291"  title="Salary: $7400000, Year: 3, Status: 3, Info: , Week 3: at Broncos Sun 4:15 p.m. ET" class="position_qb">Manning, Peyton IND QB</a></td><td class="points tot">49.61</td><td class="points avg">24.805</td><td class="points"><a href="detailed?L=34793&amp;W=1&amp;P=3291&amp;YEAR=2010">26.66</a></td> 
<td class="points"><a href="detailed?L=34793&amp;W=2&amp;P=3291&amp;YEAR=2010">22.95</a></td> 
<td class="status"><a title="Owner: Robert M. Cavezza "  class="myfranchise franchise_0013" href="http://football22.myfantasyleague.com/2010/options?L=34793&amp;F=0013&amp;O=01">The Bullies</a></td><td class="week">7</td><td class="salary">$7400000</td></tr> 

编辑:jquery 答案发生了什么?我正要实现它,但它消失了

What's the best way to parse this data? Should I use regex or something else? The data is in html, but I found it from a website and will be parsing this and only this string (note: string is much longer - over 1,300 instances - only two below) - note I use php & jquery for most web programming.

I only need to extract the data in the second td and only the anchor text inside the anchor - in instance 1, it's: Best, Jahvid DET RB

And I only need to run this loop one time.

<tr class="oddtablerow"><td class="rank">1.</td><td class="rank">1.</td><td class="player"><a href="http://football22.myfantasyleague.com/2010/player?L=34793&P=9839"  title="Salary: $2250000, Year: 3, Status: 3, Info: Drafted 10 1:04 Team, Week 3: at Vikings Sun 1:00 p.m. ET" class="position_rb">Best, Jahvid DET RB</a> (R) </td><td class="points tot">53.90</td><td class="points avg">26.950</td><td class="points"><a href="detailed?L=34793&W=1&P=9839&YEAR=2010">17.55</a></td> 
<td class="points"><a href="detailed?L=34793&W=2&P=9839&YEAR=2010">36.35</a></td> 
<td class="status"><a title="Owner: William Gold"  class="franchise_0009" href="http://football22.myfantasyleague.com/2010/options?L=34793&F=0009&O=01">Team Name</a> - <a href="options?L=34793&O=05&FRANCHISE=0013,0009&PLAYER=9839,">Trade</a></td><td class="week">7</td><td class="salary">$2250000</td></tr> 
<tr class="eventablerow myfranchise "><td class="rank">2.</td><td class="rank">2.</td><td class="player"><a href="http://football22.myfantasyleague.com/2010/player?L=34793&P=3291"  title="Salary: $7400000, Year: 3, Status: 3, Info: , Week 3: at Broncos Sun 4:15 p.m. ET" class="position_qb">Manning, Peyton IND QB</a></td><td class="points tot">49.61</td><td class="points avg">24.805</td><td class="points"><a href="detailed?L=34793&W=1&P=3291&YEAR=2010">26.66</a></td> 
<td class="points"><a href="detailed?L=34793&W=2&P=3291&YEAR=2010">22.95</a></td> 
<td class="status"><a title="Owner: Robert M. Cavezza "  class="myfranchise franchise_0013" href="http://football22.myfantasyleague.com/2010/options?L=34793&F=0013&O=01">The Bullies</a></td><td class="week">7</td><td class="salary">$7400000</td></tr> 

Edit: What happened to the jquery answer? I was about to implement it but it disappeared

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-10-01 13:32:04

如果您正在寻找执行速度最快的解决方案 XmlReader 是最快的 XML 解析器之一。使用起来有点困难,然后是其他解决方案,例如 DOM ,但由于您想要解析大量条目,因此性能可能很重要。

否则 DOM 使用起来非常简单。您可以在 这个答案我在另一个问题上给出过。

如果您想将内容作为字符串加载,请按以下步骤操作:

XMLReader

$foo = new XMLReader();
$foo->xml($yourStringHere);

DOMDocument

$foo = new DOMDocument();
$foo->loadHTML($yourStringHere);

If you are looking for a solution with the fastest execution speed XmlReader is one of the fastest XML parser. It is a bit harder to use, then other solution such as DOM, but since you want to parse a lot of entry, performance is probably important.

Otherwise DOM is pretty simple to use. You can find a simple example of how to use in this answer I gave on an other question.

If you want to load up your content as a string here's how you do it :

XMLReader

$foo = new XMLReader();
$foo->xml($yourStringHere);

DOMDocument

$foo = new DOMDocument();
$foo->loadHTML($yourStringHere);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文