不确定如何将我已经拥有的代码转换为html dom

时间:2013-05-22 02:05:06

标签: php dom

我已经拥有的抓取代码不起作用所以我搜索并发现我需要使用DOM而且我不确定如何在读取之后实现我已经拥有的DOM。我担心会破坏某些东西。任何帮助/教程都非常感谢。

// get input
$link = post('link1');
$category = post('category');
$time = post('time');

// markers
$findme1 = 'https://www.mturk.com/mturk/preview?groupId=';
$findme2 = '<span class="reward">';
$findme3 = '</span>';

// check if link is correct
$rightlink = strpos($link, $findme1);
// if link is correct
  if ($rightlink !== false)
{
    // get html from link
    $html = file($link);

    // iterate through html
    foreach ($html as $i => $line)
    {
        // set title
        if($i == 640) $title = htmlentities($line);

        // set requester
        if($i==669) $requester = htmlentities($line);

        if($i==678)
        {
            // modify the line and save as reward
            $line_modified = str_replace($findme2, '', $line);
            $line_modified = str_replace($findme3, '', $line_modified);
            $reward = htmlentities($line_modified);
        }

        // set qualifications
        if($i==711) $q = htmlentities($line);
    }

1 个答案:

答案 0 :(得分:0)

尝试PHP Simple HTML DOM Parser,它会让您的生活更轻松,阅读文档并做您想做的任何事情。如果您熟悉jQuery,那么它已经掌握了。请看下面给出的示例

include('simple_html_dom.php');
$html = file_get_html('https://requester.mturk.com/');
foreach($html->find('a') as $link){
    echo $link . '<br />';
}

该代码从https://requester.mturk.com获取所有数据,并使用foreach循环打印所有链接。我认为代码是自描述的。