網路爬蟲——URL解析

針對上節所講的對url的解析程序,對爬蟲資料庫而言是十分關鍵的,以下給各位蟲友們兩個解析URL的方案,第一個適合水平中等及以上的爬友,plan B則是適合入門級的爬友,歡迎各位收藏。

Plan A

#include <stdio.h> //printf

#include <string.h> //strchr strncmp

#include <malloc.h> //malloc free

//將source開始空間的以NULL結尾的字元拷貝到dest中

char*dup_str(const char*source)

{

if(source==NULL)

return NULL;

Advertisements

int len = strlen(source);

char *dest = (char*)malloc(len+1); //申請len+1個位元組的內存,並把申請到的內存首地址返回賦給dest指針

memcpy(dest,source,len+1); //這是一個內存操作的函數,就是把source變數中的內容複製到dest中,複製的大小就是後面那個數字,代表位元組。

return dest;

}

//函數功能:解析URL

//參數:host帶回主機字元串,protocl協議,abs_path帶回絕對路徑

void parse_URL(const char*URL,const char*protocl,char*&host,char*&abs_path)

Advertisements

{

if(URL == NULL)

return ;

char *url_dup = dup_str(URL); //定義字元指針變數 URL_dup,它的值來源於上方所定義的函數

char *p_slash = NULL; //主機IP后第一個斜杠的位置

char *start = 0; //記錄www開始的位置

if(strncmp(url_dup,protocl,strlen(protocl))==0) //將URL_dup指針所代表的參量與protocl指針帶的參量比較,從第一個字母開始比較若差值為0,則繼續比較下一個,直到為'\0'

{

start = url_dup+strlen(protocl)+3; //www的位置起始於protocl結束后的第三個字元,因為Protocl後面是"://"

p_slash = strchr(start,'/'); //char *strchr(const char *s,char c);查找字元串start中首次出現字元'/'的位置

if(p_slash != NULL) //如果p_slash的值不是NULL,即www開始后找到了'/'字元

{

abs_path= dup_str(p_slash); //將絕對路徑(即資源的訪問路徑)通過dup_str函數得到,即將p_slash的地址作為實參傳給函數的形參,得到'/'及以後的字元賦給abs_path變數

*p_slash = '\0'; //賦值給abs_path之後,p_slash的值返回0

}

else

{

abs_path= dup_str("/"); //如果www以後沒有查找到'/'字元,那麼abs_pathway的值就是'/'

}

p_colon = strchr(start,':'); //埠是www開始以後第一個「:」之後的字元

host = dup_str(start); //主機字元串

if(url_dup != NULL) //對之前url_dup的釋放過程

{

free(url_dup); //釋放malloc函數給指針變數分配的內存空間的函數

url_dup = NULL; //url_dup的值變為空

}

}

int main()

{

char *URL = "https://www.ncbi.nlm.nih.gov/pubmed/?term=embryo+development";

char*abs_path = NULL;

char*host = NULL;

parse_URL(URL,"https",host,port,abs_path);

printf("主機IP:%s\n",host);

printf("訪問路徑:%s\n",abs_path);

//需要釋放host,abs_path

if(host!=NULL)

{

free(host);

host = NULL;

}

if(abs_path!=NULL)

{

free(abs_path);

abs_path=NULL;

}

getchar();

}

Plan B

#include<stdio.h>

#include<string.h>

#include<ctype.h>

int main()

{

char *URL;

char *website;

char *without_protocl;

char *mainframe;

char *catalog;

char *pathway;

char *keyword;

int i,len;

URL="https://www.ncbi.nlm.nih.gov/pubmed/?term=embryo+development";

website=URL;

without_protocl=strchr(website,':');

if(without_protocl==NULL)

{

printf("URL輸入有誤\n");

return 0;

}

len=website-without_protocl;

for (i=0;i<len;i++)

{

if(isalpha(website==0))

{

printf("URL輸入有誤\n");

return 0;

}

}

printf("網路協議:");

for(i=0;i<len;i++)

{

printf("%c",website[i]);

}

printf("\n");

without_protocl++;

for (i=0;i<2;i++)

{

if(website[i]!='/')

{

printf("URL輸入有誤\n");

return 0;

}

}

without_protocl++;

mainframe=strchr(without_protocl,'/');

mainframe++;

catalog=strchr(mainframe,'/');

len=mainframe-catalog;

printf("主機IP:");

for (i=0;i<len;i++)

printf("%c",mainframe[i]);

printf("\n");

catalog++;

pathway=strchr(catalog,'/');

len=catalog-pathway;

printf("目錄:");

for (i=0;i<len;i++)

printf("%c",catalog[i]);

printf("\n");

pathway++;

keyword=strchr(pathway,'=');

if(keyword==NULL)

{

printf("網址是目錄首頁");

printf("\n");

}

else keyword++;

printf("keyword:");

for (i=0;keyword!=NULL;i++)

{

printf("%c",keyword[i]);

}

}

希望大家喜歡,下一講我會從http協議入手講解爬蟲的網頁獲取。各位爬友積極訂閱,積極推廣。要是有更好的代碼歡迎交流。

Advertisements

你可能會喜歡