通過python 爬取網址url 自動提交百度


通過python 爬取網址url 自動提交百度

昨天同事說,可以手動提交百度這樣索引量會上去。

然后想了下。是不是應該弄一個py 然后自動提交呢?想了下。還是弄一個把

python 代碼如下:

 

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."

 

 
         

 

執行完之后就會有如下url

再弄一個主動提交的腳本,我進入百度錄入的網址找到自己提交的地址

 

寫了一個垃圾腳本,本來想融入到py中。但是想了下,還是別了

 

[root@iz2zej11i9rkbm4yvq43laz script]# cat baiduurl.sh 
cd /script && curl -H 'Content-Type:text/plain' --data-binary @result.txt  "http://data.zz.baidu.com/urls?site=https://www.o2oxy.cn&token=P03781O3s6Ee"
 && curl -H 'Content-Type:text/plain' --data-binary @result.txt  "http://data.zz.baidu.com/urls?site=https://www.o2oxy.cn&token=P03781O3s6E"

執行結果如下:

[root@iz2zej11i9rkbm4yvq43laz script]# sh baiduurl.sh 
{"remain":4993750,"success":455}{"remain":4993295,"success":455}

然后做了一個計划任務

執行一下。獲取網址url 比較慢,可能十分鍾把

 

 

唔。完美!!!!!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM