以下这个是 分享一个页面上的 爬虫图片方法,这个网页爬取图片是比较入门的,没有动态 也没有反爬虫机制 不过还是要写一个 申请头
import requests,bs4,os
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
tupiao_name = input('你要搜索什么图片?')
q = input('你要下载几张图片?输入:1,下载20张;输入:2,下载40张。以此类推')
data = {
'keyword': tupiao_name,
'nums': '16'
}
tupiao_list = []
num = 0
dir_name = 'D:\\图片\\'
for x in range(int(q)):
url = 'https://www.ivsky.com/search.php?q='+tupiao_name+'&PageNo='+str(x)
res = requests.get(url,data= data,headers=headers)
bs = bs4.BeautifulSoup(res.text,'html.parser')
soup = bs.find('ul',class_='pli').find_all('li')
for itme in soup:
url_1 = 'https://www.ivsky.com' + itme.find_all('a')[0]['href']
res_1 = requests.get(url_1,data= data,headers=headers)
bs_1 = bs4.BeautifulSoup(res_1.text,'html.parser')
tupiao_dizhi = 'https:' + bs_1.find(id='imgis')['src']
tupiao_list.append(tupiao_dizhi)
if not os.path.exists(dir_name):
os.mkdir(dir_name)
for a in tupiao_list:
num = num + 1
img = requests.get(a)
f = open(dir_name+tupiao_name+str(num)+'.jpg','ab')
print('--------当前下载第'+str(num)+'张'+tupiao_name+'图片-----------')
f.write(img.content)
f.close()
print(tupiao_list)
可以参考一下看看,刚入CSDN博客 多多指教哈哈哈