自己網站因數據比較多,趁晚上沒事就寫了一個通過python爬取url自動提交給百度,實現網站全站提交的思路,代碼實現很簡單,因為編寫時間倉儲,難免有些bug,可以放在服務器上配置下定時爬取提交。
import os import re import shutil REJECT_FILETYPE = 'rar,7z,css,js,jpg,jpeg,gif,bmp,png,swf,exe' #定義爬蟲過程中不下載的文件類型 def getinfo(webaddress): #'#通過用戶輸入的網址連接上網絡協議,得到URL我這里是我自己的域名 global REJECT_FILETYPE url = 'http://'+webaddress+'/' #網址的url地址 print 'Getting>>>>> '+url websitefilepath = os.path.abspath('.')+'/'+webaddress #通過函數os.path.abspath得到當前程序所在的絕對路徑,然后搭配用戶所輸入的網址得到用於存儲下載網頁的文件夾 if os.path.exists(websitefilepath): #如果此文件夾已經存在就將其刪除,原因是如果它存在,那么爬蟲將不成功 shutil.rmtree(websitefilepath) #shutil.rmtree函數用於刪除文件夾(其中含有文件) outputfilepath = os.path.abspath('.')+'/'+'output.txt' #在當前文件夾下創建一個過渡性質的文件output.txt fobj = open(outputfilepath,'w+') command = 'wget -r -m -nv --reject='+REJECT_FILETYPE+' -o '+outputfilepath+' '+url #利用wget命令爬取網站 tmp0 = os.popen(command).readlines() #函數os.popen執行命令並且將運行結果存儲在變量tmp0中 print >> fobj,tmp0 #寫入output.txt中 allinfo = fobj.read() target_url = re.compile(r'\".*?\"',re.DOTALL).findall(allinfo) #通過正則表達式篩選出得到的網址 print target_url target_num = len(target_url) fobj1 = open('result.txt','w') #在本目錄下創建一個result.txt文件,里面存儲最終得到的內容 for i in range(target_num): if len(target_url[i][1:-1])<70: # 這個target_url 是一個字典形式的,如果url 長度大於70 就不會記錄到里面 print >> fobj1,target_url[i][1:-1] #寫入到文件中 else: print "NO" fobj.close() fobj1.close() if os.path.exists(outputfilepath): #將過渡文件output.txt刪除 os.remove(outputfilepath) #刪除 if __name__=="__main__": webaddress = raw_input("Input the Website Address(without \"http:\")>") getinfo(webaddress) print "Well Done."
然后進入百度主動提交欄目,找到api接口,提交下數據即可