原网页 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)
写个正则,错了十几次,我好垃圾