[PHP] Network Disk search engine-collect and crawl Baidu Network Disk shared files to achieve Network Disk search (2 ),

Source: Internet
Author: User

[PHP] Network Disk search engine-collect and crawl Baidu Network Disk shared files to achieve Network Disk search (2 ),

Prerequisites: Recently, PHP was used to implement a simple online storage search program, which is associated with a public platform named online storage. You can enter a keyword using the public account. The public account returns the corresponding online storage. It is such a simple function. Similar to many online search websites, my collection and search programs are implemented in PHP. The full text and Word Segmentation search sections use the open-source software xunsearch.

In the previous article ([PHP] online storage search engine-acquisition and crawling of Baidu online storage shared files for online storage search), I highlighted how to obtain a large number of users of Baidu online storage, this article describes how to obtain a shared list of users on a specified network disk. The same principle is to find Baidu's interface for obtaining the sharing list and then go through the loop.

 

Shared search APIs

Find a share page for an online storage user and click the following link to view the request interface. This is the interface for obtaining the share list.

The whole request url is this https://pan.baidu.com/pcloud/feed/getsharelist? T = 1493892795526 & category = 0 & auth_type = 1 & request_location = pai_home & start = 60 & limit = 60 & query_uk = 4162539356 & channel = chunlei & clienttype = 0 & web = 1 & logid = MTQ5Mzg5Mjc5NTUyNzAuOTEwNDc2NTU1NTgyMTM1OQ ==& bdstoken = bc329b0677cad94231e973953a09b46f

 

Call interfaces to obtain data

Use php curl to request this interface to see if data can be obtained. After the test, {"errno": 2, "request_id": 1775381927} is returned, and no data is obtained. This is because Baidu has imposed restrictions on the Referer in the header information. I changed the Referer to a http://www.baidu.com and I can get the data. Interface parameters can also be simplified to https://pan.baidu.com/pcloud/feed/getsharelist? & Auth_type = 1 & request_location = pai_home & start = 60 & limit = 60 & query_uk = 4162539356

The test code is as follows:

<? Php/** get share list */class TextsSpider {/*** send request */public function sendRequest ($ url, $ data = null, $ header = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, success, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE ); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} if (! Empty ($ header) {curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header);} curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ output = curl_exec ($ curl ); curl_close ($ curl); return $ output; }}$ textsSpider = new TextsSpider (); $ header = array ('referer: http://www.baidu.com '); $ str = $ textsSpider-> sendRequest ("https://pan.baidu.com/pcloud/feed/getsharelist? & Auth_type = 1 & request_location = pai_home & start = 60 & limit = 60 & query_uk = 4162539356 ", null, $ header); echo $ str;

The json result of the shared list is as follows:

{"Errno": 0, "request_id": 1985680203, "total_count": 1025, "records": [{"feed_type": "share", "category": 6, "public": "1", "your id": "98963537", "data_id": "1799945104803474515", "title": "psychic girl" 2017. synchronization platform view (end to be deleted) "," third ": 0," clienttype ": 0," filecount ": 1," uk ": 4162539356," username ": "a20 **** 3762", "feed_time": 1493626027308, "desc": "", "avatar_url": "https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/01f8831f.jpg", "dir_cnt": 1, "filelist ": [{"server_filename": "psychic girl 2017. synchronization platform view (end to be deleted) "," category ": 6," isdir ": 1," size ": 1024," fs_id ": 98994643773159," path ": "% 2F % E3 % 80% 8A % E9 % 80% 9A % E7 % 81% B5 % E5 % B0 % 91% E5 % A5 % B3 % E3 % 80% 8B2017.% E5 % 90% 8C % E6 % AD % A5 % E5 % 8F % B0 % E8 % A7 % 86% EF % BC % 88% E5 % AE % 8C % E7 % BB % 93% E5 % BE % 85% E5 % 88% A0 % EF % BC % 89 ", "md5": "0", "sign": "success", "time_stamp": 1493895381}], "source_uid": "528742401", "source_id": "98963537 ", "Signed URL": "1pKPCF0J", "vCnt": 356, "dCnt": 29, "tCnt": 184 },{ "source_uid": "528742401", "source_id ": "152434783", "invalid URL": "1 qYdhFkC", "vCnt": 1022, "dCnt": 29, "tCnt": 345}]}

Like the previous one, a comprehensive search site can save all the useful data. I just want to make the simplest one, as long as the title and upload ID

The download page url for each shared file is like this: http://pan.baidu.com/share/link? Your id = {$ your id} & uk = {$ uk}. You only need the user id and sharing id to spell out the download url.

 

Generate paging interface URL

Assume that you have shared a maximum of 30000 entries, 60 entries per page, and 500 pages. This way, the url can be generated as follows:

<? Php/** get share list */class TextsSpider {private $ pages = 500; // Number of pages private $ start = 60; // The number of pages per page/*** generate the paging interface url */public function makeUrl ($ rootUk) {$ urls = array (); for ($ I = 0; $ I <= $ this-> pages; $ I ++) {$ start = $ this-> start * $ I; $ url = "https://pan.baidu.com/pcloud/feed/getsharelist? & Auth_type = 1 & request_location = pai_home & start = {$ start} & limit = {$ this-> start} & query_uk = {$ rootUk }"; $ urls [] = $ url;} return $ urls ;}}$ textsSpider = new TextsSpider (); $ urls = $ textsSpider-> makeUrl (4162539356 ); print_r ($ urls );

The results of the paginated url are as follows:

Array(    [0] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=0&limit=60&query_uk=4162539356    [1] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=60&limit=60&query_uk=4162539356    [2] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=120&limit=60&query_uk=4162539356    [3] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=180&limit=60&query_uk=4162539356    [4] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=240&limit=60&query_uk=4162539356    [5] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=300&limit=60&query_uk=4162539356    [6] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=360&limit=60&query_uk=4162539356    [7] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=420&limit=60&query_uk=4162539356    [8] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=480&limit=60&query_uk=4162539356    [9] => https://pan.baidu.com/pcloud/feed/getsharelist?&auth_type=1&request_location=share_home&start=540&limit=60&query_uk=4162539356

 

Data Table Storage Structure

CREATE TABLE `texts` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `title` varchar(255) NOT NULL DEFAULT '',  `url` varchar(255) NOT NULL DEFAULT '',  `time` int(10) unsigned NOT NULL DEFAULT '0',  PRIMARY KEY (`id`),  KEY `title` (`title`(250))) ENGINE=MyISAM

 

When reading cyclically, you should note that each interval is a certain period of time to prevent being blocked.

Next, we will introduce the xunsearch word segmentation and full-text search and the complete code.

Demonstration address, follow the public number: online storage novels, or scan the following QR code

The complete code for getting the uk and storing it in the database in the previous loop is as follows:

<? Php/** get subscriber */class UkSpider {private $ pages; // The number of sub-pages private $ start = 24; // The number of each page private $ db = null; // database public function _ construct ($ pages = 100) {$ this-> pages = $ pages; $ this-> db = new PDO ("mysql: host = localhost; dbname = pan "," root "," root "); $ this-> db-> query ('set names utf8 ');} /*** generate the paging interface url */public function makeUrl ($ rootUk) {$ urls = array (); for ($ I = 0; $ I <= $ this-> pages; $ I ++) {$ start = $ this-> start * $ I; $ url =" Https://pan.baidu.com/pcloud/friend/getfollowlist? Query_uk = {$ rootUk} & limit = {$ this-> start} & start = {$ start} "; $ urls [] = $ url;} return $ urls ;} /*** obtain the subscription user id based on the URL */public function getFollowsByUrl ($ url) {$ result = $ this-> sendRequest ($ url); $ arr = json_decode ($ result, true); if (empty ($ arr) |! Isset ($ arr ['follow _ list']) {return;} $ ret = array (); foreach ($ arr ['follow _ list'] as $ fan) {$ ret [] = $ fan ['follow _ uk '];} return $ ret;}/*** send request */public function sendRequest ($ url, $ data = null, $ header = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} if (! Empty ($ header) {curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header);} curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ output = curl_exec ($ curl ); curl_close ($ curl); return $ output;}/* store the obtained uks into data */public function addUks ($ uks) {foreach ($ uks as $ uk) {$ SQL = "insert into uks (uk) values ({$ uk})"; $ this-> db-> prepare ($ SQL)-> execute ();}} /* obtain all the subscriptions of a user and import them to the database */public function sleepGetByUk ($ uk) {$ urls = $ this-> makeUrl ($ uk ); // $ this-> updateUkFollow ($ uk); // loop pagination url foreach ($ urls as $ url) {echo "loading :". $ url. "\ r \ n"; // random sleep for 7 to 11 seconds $ second = rand (7,11); echo "sleep... {$ second} s \ r \ n "; sleep ($ second); // initiate a request $ followList = $ this-> getFollowsByUrl ($ url ); // if no data exists, stop the request if (empty ($ followList) {break;} $ this-> addUks ($ followList );}} /* Get the uk with get_follow = 0 from the database */public function getUksFromDb () {$ thing = $ this-> db-> prepare ("select * from uks where get_follow = 0"); $ thing-> execute (); $ uks = $ something-> fetchAll (PDO: FETCH_ASSOC); $ result = array (); foreach ($ uks as $ key => $ uk) {$ result [] = $ uk ['uk'];} return $ result;}/* Set follow to 1 */public function updateUkFollow ($ uk) {$ SQL = "UPDATE uks SET get_follow = 1 where uk = {$ uk}"; $ this-> db-> prepare ($ SQL)-> execute ();}} $ ukSpider = new UkSpider (); $ uks = $ ukSpider-> getUksFromDb (); foreach ($ uks as $ uk) {$ ukSpider-> sleepGetByUk ($ uk );}

 

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.