代码
from urllib.request import urlopen
from bs4 import BeautifulSoup
import os
f = open ('2.txt')
for i in f:
root = "D:\Python1"
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(i)
with open(path,'wb')as f:
f.write(r.content)
f.close()
print("保存成功")
else:
print("保存失败")
except:
print("爬取失败")
from urllib.request import urlopen
from bs4 import BeautifulSoup
import os
f = open ('2.txt')
for i in f:
root = "D:\Python1"
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(i)
with open(path,'wb')as f:
f.write(r.content)
f.close()
print("保存成功")
else:
print("保存失败")
except:
print("爬取失败")