从刮擦中删除

发布于 2024-11-10 23:07:04 字数 1110 浏览 2 评论 0原文

嘿大家, 我已经成功创建了一个网站抓取工具,从唱片行业网站获取前 40 名,但是我正在抓取的表中的一列有时可能不存在。基本上我需要的是一种从我的刮擦中删除任何此类实例的方法:

<td><img src="/images/bullet_red.gif" width="8" height="8" title="Red Dot" /></td>

这是迄今为止我从教程中获得的内容。

$url = "http://www.ariacharts.com.au/pages/charts_display_singles.asp?chart=1U50";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");

$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content,'<table class="chartTable"');
$end = strpos($content,'</table>',$start) + 8;

$table = substr($content,$start,$end-$start);

preg_match_all("|<tr(.*)</tr>|U",$table,$rows);

foreach ($rows[0] as $row){

if ((strpos($row,'<th')===false)){

    preg_match_all("|<td(.*)</td>|U",$row,$cells);

    $number = strip_tags($cells[0][1]);

    $name = strip_tags($cells[0][5]);

    $artist = strip_tags($cells[0][6]);

    $name = strtolower($name);
    $name = ucwords($name);

    echo "{$artist} - {$name} - Number {$number} <br>\n";

}

}

Hey all,
I've successfully created a website scraper getting the top 40 from the record industry website, however one of the columns in the table I'm scraping might sometimes not be there. Basically what I need is a way to remove any instances of this from my scrape:

<td><img src="/images/bullet_red.gif" width="8" height="8" title="Red Dot" /></td>

Here's what I've got from a tutorial so far.

$url = "http://www.ariacharts.com.au/pages/charts_display_singles.asp?chart=1U50";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");

$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content,'<table class="chartTable"');
$end = strpos($content,'</table>',$start) + 8;

$table = substr($content,$start,$end-$start);

preg_match_all("|<tr(.*)</tr>|U",$table,$rows);

foreach ($rows[0] as $row){

if ((strpos($row,'<th')===false)){

    preg_match_all("|<td(.*)</td>|U",$row,$cells);

    $number = strip_tags($cells[0][1]);

    $name = strip_tags($cells[0][5]);

    $artist = strip_tags($cells[0][6]);

    $name = strtolower($name);
    $name = ucwords($name);

    echo "{$artist} - {$name} - Number {$number} <br>\n";

}

}

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

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

发布评论

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

评论(2

卖梦商人 2024-11-17 23:07:04

尝试使用 PHP 简单 HTML DOM 解析器而不是复杂的正则表达式 http://simplehtmldom.sourceforge.net/

require_once 'simple_html_dom.php';

$html = file_get_html('http://www.ariacharts.com.au/pages/charts_display_singles.asp?chart=1U50');
$table = $html->find('table.chartTable');

foreach ($table[0]->find('tr') as $row) {
    $columns = $row->find('td');
    if (sizeof($columns) < 7) continue;

    $number = $columns[1]->plaintext;
    $name = ucwords($columns[6]->plaintext);
    $artist = $columns[7]->plaintext;

    echo "$artist - $name - Number $number <br />\n";
}

Try using PHP Simple HTML DOM Parser instead of complex regex http://simplehtmldom.sourceforge.net/

require_once 'simple_html_dom.php';

$html = file_get_html('http://www.ariacharts.com.au/pages/charts_display_singles.asp?chart=1U50');
$table = $html->find('table.chartTable');

foreach ($table[0]->find('tr') as $row) {
    $columns = $row->find('td');
    if (sizeof($columns) < 7) continue;

    $number = $columns[1]->plaintext;
    $name = ucwords($columns[6]->plaintext);
    $artist = $columns[7]->plaintext;

    echo "$artist - $name - Number $number <br />\n";
}
孤星 2024-11-17 23:07:04

对于您想要的快速而肮脏的方法,请将此代码放在声明“start”变量之前:

$content = str_replace('<td><img src="/images/bullet_red.gif" width="8" height="8" title="Red Dot" /></td>', '', $content);

For the quick and dirty method you want, put this code before you declare the "start" variable:

$content = str_replace('<td><img src="/images/bullet_red.gif" width="8" height="8" title="Red Dot" /></td>', '', $content);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文