通過python 爬取網址url 自動認可百度

來源:互聯網
上載者:User

標籤:--   十分   正則   glob   with   成功   input   將不   type   

通過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)  #通過Regex篩選出得到的網址     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中。但是想了下,還是別了

 

[[email protected] 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"

執行結果如下:

[[email protected] script]# sh baiduurl.sh {"remain":4993750,"success":455}{"remain":4993295,"success":455}

然後做了一個計劃任務

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

 

 

唔。完美!!!!!

 

 

通過python 爬取網址url 自動認可百度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.