이전 기사에서는 뉴스 정보 페이지의 목록 데이터를 수집했습니다. 다음 단계는 데이터베이스에서 수집해야 하는 URL을 읽고 페이지를 크롤링하는 것입니다
새 콘텐츠 테이블 만들기
그러나 한 가지 주의할 점은 id=9, id=11인 경우 데이터 테이블에 id 불연속성이 있을 수 있으므로 더 이상 URL을 수집하는 증분 방법을 사용할 수 없다는 것입니다. 수집됨, 때로는 URL이 비어 있어 빈 필드가 수집될 수 있습니다.
여기서 사용되는 기술 중 하나는 데이터베이스의 쿼리문입니다. 첫 번째 데이터를 수집할 때 데이터베이스에 이 ID보다 큰 ID 번호가 있는지 확인합니다. 위의 정보를 쿼리하세요.
구체적인 코드는 다음과 같습니다.
<?php include_once("conn.php"); $id=(int)$_GET['id']; $sql="select * from list where id=$id"; $result=mysql_query($sql); $row=mysql_fetch_array($result);//取得对应的url地址 $content=file_get_contents($row['url']); $pattern="/<dd class=\"dataWrap\">(.*)<\/dd>/iUs"; preg_match($pattern, $content,$info);//获取内容存放info echo $title=$row[1]."<br/>"; echo $content=$info[0]."<hr/>"; //插入数据库 $add="insert into content(title,content) value('$title','$content')"; mysql_query($add); $sql2="select * from list where id>$id order by id asc limit 1"; $result2=mysql_query($sql2); $row2=mysql_fetch_array($result2);//取得对应的url地址 if($row2['id']){ echo "<script>window.location='content.php?id=$row2[0]'</script>"; } ?>
이런 식으로 우리가 원하는 뉴스 콘텐츠가 수집되어 데이터베이스에 저장되었습니다. 다음으로 데이터의 일부 스타일만 정리하면 됩니다.