爬了一手boostarp的例子

原网页 https://getbootstrap.com/docs/4.4/examples/

#html
import requests
import time
import re
from bs4 import BeautifulSoup
header={
'cookie':'--',
'user-agent':'--'
}
def getHTMLText(url): #照抄就完事了
    try:
        r = requests.get(url=url, timeout=30)
        r.raise_for_status()  # 产生异常信息
        r.encoding = r.apparent_encoding  # 改变编码
        return r.text
    except Exception as e:
        print("异常对象的内容是:%s" % e)
        return ""
def getStochList(lst,lstName,stockURL):
    html=getHTMLText(stockURL) #得到源码
    soup=BeautifulSoup(html,'html.parser') #解析
    for tag in soup.find_all(href=re.compile('/docs/4.4/examples/.*/')):
        string='https://getbootstrap.com/'+re.search(r'docs/4.4/examples/.*/', str(tag)).group()
        print(string)
        lstName.append(string[43:-2])
        lst.append(string)
    return ""
def getStochInfo(lst,lstName, path):
    for i in range(0, len(lst)):
        with open(path+lstName[i]+".html",'w',encoding="utf-8") as f:
            print("写入")
            f.write(getHTMLText(lst[i]))
    return ""
def main(url,path):
    List = []
    Listname=[]
    getStochList(List,Listname, url)  # 解析网页
    getStochInfo(List,Listname, path)  # 下载文件
if __name__ == '__main__':
    url='https://getbootstrap.com/docs/4.4/examples/'

    path='D://deskpe//boostarp//' #保存路径
    begin=time.perf_counter()
    main(url,path)
    time.sleep(3)
    end=time.perf_counter()-begin
    print(end)

记得复制一手输出的html网页,爬取css的时候要用,别问我为什么分开坐,懒

#css
import requests
import time
import re
from bs4 import BeautifulSoup
header={
'cookie':'--',
'user-agent':'--'
}
def getHTMLText(url): #照抄就完事了
    try:
        r = requests.get(url=url, timeout=30)
        r.raise_for_status()  # 产生异常信息
        r.encoding = r.apparent_encoding  # 改变编码
        return r.text
    except Exception as e:
        print("异常对象的内容是:%s" % e)
        return ""
def getStochList(lst,lstName):
    html=getHTMLText(lst) #得到源码
    soup=BeautifulSoup(html,'html.parser') #解析
    for tag in soup.find_all(href=re.compile('.*\.css')):
        string=re.search(r'.*\.css', str(tag)).group()
        if "bootstrap.min.css" not in string:
            print(lst+string[string.index("\"")+1:])
            lstName.append(lst+string[string.index("\"")+1:])
    return ""
def getStochInfo(lstName, path):
    for i in range(0, len(lstName)):
        with open(path+lstName[i][lstName[i].rindex("/")+1:]+".",'w',encoding="utf-8") as f:
            f.write(getHTMLText(lstName[i]))
    return ""

def main(path):
    List = open("html.txt","r",encoding="utf-8").readlines()
    for i in range(len(List)):
    	if "\n" in List[i]: #清除换行符
        	List[i]=List[i][:-1]
    Listname=[]
    for i in range( len(List)):
        getStochList(List[i],Listname)  # 解析网页
    getStochInfo(Listname, path)  # 下载文件
if __name__ == '__main__':
    path='D://deskpe//boostarp//' #保存路径
    begin=time.perf_counter()
    main(path)
    time.sleep(3)
    end=time.perf_counter()-begin
    print(end)

写个正则,错了十几次,我好垃圾

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值