PHP Forum Acquisition Program simulation Landing, crawl page implementation code _php instance

Source: Internet
Author: User
Copy Code code as follows:

<?php
Wu Yanjun
2009-06-27
Acquisition program PHP
Set_time_limit (0);
Cookie Save Directory
$cookie _jar = '/tmp/cookie.tmp ';
/* Function------------------------------------------------------------------------------------------------------------ */
Simulate request data
function request ($url, $postfields, $cookie _jar, $referer) {
$ch = Curl_init ();
$options = Array (curlopt_url => $url,
Curlopt_header => 0,
Curlopt_nobody => 0,
Curlopt_port => 80,
Curlopt_post => 1,
Curlopt_postfields => $postfields,
Curlopt_returntransfer => 1,
Curlopt_followlocation => 1,
Curlopt_cookiejar => $cookie _jar,
Curlopt_cookiefile => $cookie _jar,
Curlopt_referer => $referer
);
Curl_setopt_array ($ch, $options);
$code = curl_exec ($ch);
Curl_close ($ch);
return $code;
}
Get a list of posts
function Getthreadslist ($code) {
Preg_match_all ('/<!--[. | \r|\n]*? <a href=\ "viewthread.php\?tid= (\d+)/', $code, $threads);
return $threads [1];
}
Determine if the post exists
function Isexits ($code) {
Preg_match ('/<p> The specified topic does not exist or has been deleted or is being audited, please return. <\/p>/', $code, $error);
return isset ($error [0]) false:true;
}
Get post title
function GetTitle ($code) {
Preg_match ('/$title = $title _tmp[0];
return $title;
}
Get post Author:
function Getauthor ($code) {
Preg_match ('/<a href=\ ' space.php\?uid=\d+\ "target=\" _blank\ "id=\" userinfo\d+\ "onmouseover=\" showMenu\ (this\. id\) \ ">.+/', $code, $author _tmp);"
$author = Strip_tags ($author _tmp[0]);
return $author;
}
Get the content published by the landlord
function getcontents ($code) {
Preg_match ('/<div id=\ "postmessage_\d+\" class=\ "t_msgfont\" > (. | \r|\n) *? <\/div>/', $code, $contents _tmp);
$contents = preg_replace ('/images\//', ' http://bbs.war3.cn/images/', $contents _tmp[0]);
return $contents;
}
Print post title
function PrintTitle ($title) {
echo "<strong> }
Output Post author
function Printauthor ($author) {
echo "<strong> }
Print post Content
function Printcontents ($contents) {
echo "<strong> }
Error
function Printerror () {
echo "<i> The post does not exist! </i> ";
}
/* Function List End---------------------------------------------------------------------------------------------------* *
/* Login Forum begin*/
$url = ' Http://bbs.war3.cn/logging.php?action=login ';
$postfields = ' loginfield=username&username=1nject10n& password=xxxxxx&questionid=0&cookietime= 315360000& referer=http://bbs.war3.cn/&loginsubmit= submission ';
Request ($url, $postfields, $cookie _jar, ");
Unset ($postfields, $url);
/* Login Forum end*/
/* Get a list of posts (posts on the first page) begin*/
$url = ' http://bbs.war3.cn/forumdisplay.php?fid=57 ';
$code = Request ($url, ', $cookie _jar, ');
$threadsList = Getthreadslist ($code);
/* Get the Post list end*/
Post sequence
$rows = 0;
/* Loop Crawl all posts source code begin*/
foreach ($threadsList as $list) {
$url = "Http://bbs.war3.cn/viewthread.php?tid= $list";
if (Isexits ($code)) {
$code = Request ($url, ', $cookie _jar, ');
$color = $rows%2==0? ' #00CCFF ': ' #FFFF33 ';
echo "<div style= ' background-color: $color ' >";
echo "$author = Getauthor ($code);
Printauthor ($author);
$title = GetTitle ($code);
PrintTitle ($title);
$contents = getcontents ($code);
Printcontents ($contents);
echo "</div>";
$rows + +;
}
Else
Printerror ();
echo "-----------------------------------------------------------------------------------------<br/> < Br/> ";
}
/* Crawl Source code end*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.