2.源代码如下:
提取url
f=open("1.txt", "r", encoding='utf-8')
f1=f.readlines()#按行读取,不加s制度第一行
f2 = open("2.txt", "w")
for line in f1:
if "<a class" in line and ".jpg" in line and ">" in line:#定位所在行
content=line.split('href="')[1].split('"')[0]#先取符合条件行第一分割的第二位字符串,再取第二分割的0号位(第一位)#取url
f2.write(content+'\n')#写入
f2.close()
f.close()
批量下载
import requests,random,os
l=os.getcwd()+"\下载图片"
os.mkdir(l)#创建存储路径
#UA伪装
uaList = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
]#UA伪装列表
userAgent=random.choice(uaList)#随机UA
headers={'user-agent':userAgent}#设置ua
f=open('2.txt',"r").readlines()
for i in f:
k = i.split('/')[-1].split("\n")[0] li=l+'\露露の'+k
fi=open(li,'wb')#创建图片载体
image= requests.get(i.split('\n')[0],headers=headers).content#图片内容
fi.write(image)#载体写入图片内容
print(k)#检查
print(k+'下载成功')
#基本完成批量下载,按照顺序.但单线下载慢,且图片在该程序所在同一文件夹
抱歉..人家不懂这些呢
硬币哒