Search Path-C # crawler Technology

Source: Internet
Author: User

Crawler
Also known as spider, it is a way to capture resources from other websites,
C #
. Net

UseCrawler
The method is as follows:

Protected
String
Getpagehtml
(String
URL
)


{


String
Pageinfo
;


Try


{


Webrequest
Myreq
= Webrequest
. Create
(URL
);



Webresponse
Myrep
= Myreq
. Getresponse
();


Streamreader
Reader
= New
Streamreader
(Myrep
. Getresponsestream
(), Encoding
. Getencoding
("Gb2312 ″
));


Pageinfo
= Reader
. Readtoend
();


}


Catch


{


Pageinfo
= ""
;


}


Return
Pageinfo
;

}

 

You can obtain a URL in the program using the above method.
Page source file.

 

However, some websites are blocked.Crawler
, You need to simulate the method obtained by the browser. The specific code is as follows:

Protected
String
Getpagehtml
(String
URL
)


{


String
Pageinfo
;


Try


{


Httpwebrequest
Myreq
= (Httpwebrequest
) Httpwebrequest
. Create
(URL
);


Myreq
. Accept
= "Image/GIF,
Image/X-xbitmap, image/JPEG, image/pjpeg,
Application/X-Shockwave-flash, application/vnd. MS-Excel,
Application/vnd. MS-PowerPoint, application/MSWord ,*/*"
;


Myreq
. Useragent
= "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1;. Net CLR 2.0.50727 )"
;


Httpwebresponse
Myrep
= (Httpwebresponse
) Myreq
. Getresponse
();


Stream
Mystream
= Myrep
. Getresponsestream
();


Streamreader
Sr
= New
Streamreader
(Mystream
, Encoding
. Default
);


Pageinfo
= Sr
. Readtoend
(). Tostring
();


}


Catch


{


Pageinfo
= ""
;


}


Return
Pageinfo
;

}

Related Article

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.