溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python爬蟲 批量下載zabbix文檔代碼實(shí)例

發(fā)布時(shí)間:2020-10-24 09:09:54 來(lái)源:腳本之家 閱讀:145 作者:NAVYSUMMER 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了python爬蟲 批量下載zabbix文檔代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

# -*- coding: UTF-8 -*-
import requests,re,time
url = 'https://www.zabbix.com/documentation/3.4/zh/manual'
base_url = 'https://www.zabbix.com/documentation/3.4/'
seconds = 1
err_url = []
def get_urls():
  res = requests.get(url)
  content = res.text
  pattern = re.compile(r"indexmenu_4848130395ca30b274d8bd.add[(]'(zh/manual.*?)[']", re.S)
  routes = pattern.findall(content)
  urls = [base_url+item for item in routes]
  return urls 
def download(url):
  download_url = url + "?do=export_pdf"
  print("當(dāng)前下載url:")
  print(download_url)
  res = requests.get(url)
  if res.status_code == 200 :
    pattern = re.compile(r"<title>(.*?)</title>", re.S)
    title = pattern.findall(res.text)[0].encode("utf-8")
    try:
      filename = title.replace('\\','-').replace('/','-').replace('"','-').replace('*','-').replace('?','-').replace(':','-').replace('<','-').replace('>','-').replace('|','-')
    except Exception:
       title = pattern.findall(res.text)[0]
    filename = title.replace('\\','-').replace('/','-').replace('"','-').replace('*','-').replace('?','-').replace(':','-').replace('<','-').replace('>','-').replace('|','-')
    file = filename + '.pdf'
    res = requests.get(download_url)
    if res.status_code == 200 :
      with open(file,"wb") as f:
        f.write(res.content)
      print('下載成功')
    else:
      print('下載失敗')
      err_url.append(download_url)
  else:
    print('獲取文件名失敗,停止當(dāng)前下載')
    err_url.append(download_url) 
def downloads(urls):
  for url in urls:
    download(url)
    time.sleep( seconds )
  if len(err_url) :
    print("下載失敗的URL:")
    print(err_url) 
def main():
  print("下載開(kāi)始")
  urls = get_urls()
  downloads(urls)
  print("下載完成") 
if __name__ == '__main__':
  main()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI