python批量下载开源软件实例代码

发布时间:2020-07-24编辑:脚本学堂
使用python批量下载软件,网上有很多开源软件,逐个下载很费时间,用python代码批量下载开源软件,用到了requests模块、gevent模块等。

通常下载一些开源软件时,会从国外的站点上找到列表,然后单个下载,考虑到linux软件包的依赖关系,有时会下载多个文件,这时就会考虑到如何在linux下批量下载软件的问题。

这里分享一例python代码,实现开源软件的批量下载,一起来看看。

python代码:
 

复制代码 代码示例:
#!/usr/bin/env python
import requests
import lxml
import gevent
from lxml import html
import os
 
from gevent import monkey; monkey.patch_os()
monkey.patch_socket()
 
from multiprocessing import Process
 
#view-source:https://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/epel/
base_url = "https://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/epel/"
url_send="https://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/"
repodata_url ="https://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/repodata/"
 
puppetlab = "http://yum.puppetlabs.com/el/6/products/x86_64/"
 
def get_rpms(url):
 
    r = requests.get( url )
    doc = html.fromstring(r.text)
    doc.make_links_absolute( url )
    links = doc.iterlinks()
    rpms = [i[2]  for i in links if i[2].endswith("rpm")]
    return rpms
 
def  get_gzs(url):
    r = requests.get(url)
    doc = html.fromstring(r.text)
    doc.make_links_absolute(url)
    links = doc.iterlinks()
    rpms = [i[2] for i  in links if i[2].endswith("gz") or i[2].endswith("bz2")]
    return rpms
 
def download(url, dir):
    r = requests.get( url )
    filename = url.split("/")[-1]
    print(filename)
    f = open( os.path.join(dir, filename) , "wb")
    f.write(r.content)
    f.close()
 
rpms = get_rpms ( puppetlab )
 
w = []
 
for i in rpms:
    print(i)
    g = gevent.spawn(download, i, "puppetlab",)
    w.append(g)
 
gevent.joinall(w)
gevent.sleep(0.3)