安装
pip install bs4
代码
import requestsfrom bs4 import BeautifulSoupimport timeurl = '/bizhitupian/weimeibizhi/'domain = ''resp = requests.get(url)resp.encoding = 'utf-8'# print(resp.text)# 把网页源代码传给bspage = BeautifulSoup(resp.text, 'html.parser')a_list = page.find('div', class_='TypeList').find_all('a')# print(a_list)for a in a_list:# 使用get可以直接拿到属性值href = domain + a.get('href')child_resp = requests.get(href)child_resp.encoding = 'utf-8'child_content = child_resp.text# 从子页面中拿图片的下载路径child_page = BeautifulSoup(child_content, 'html.parser')p = child_page.find('p', align='center')# print(p)img = p.find('img')src = img.get('src')# 下载图片img_resp = requests.get(src)# 这里拿到的是字节# img_resp.contentimg_name = src.split('/')[-1]with open('img/' + img_name, mode='wb') as f:f.write(img_resp.content)f.close()print('Pic:{} download successfully!'.format(img_name))time.sleep(1)resp.close()print('All Over!')
效果
注意
把img文件夹标志为Excluded,这样Pycharm就不会建立索引,不然会很卡
如果觉得《Python爬虫:7_BeautifulSoup4图片爬取》对你有帮助,请点赞、收藏,并留下你的观点哦!