本文最后更新于121 天前,其中的信息可能已经过时,如有错误请发送邮件到[email protected]
# 下面的要爬的网站链接和图片保存目录根据自身情况修改
注: 不同网站之间结构不同,所以需要适当的修改一下代码
import requests
import re
from bs4 import BeautifulSoup
url = 'https://umei.net/'
response = requests.get(url=url)
response.encoding = 'utf-8'
text_soup = BeautifulSoup(response.text, 'html.parser')
text_find_all = text_soup.find_all('li', attrs={'class': 'i_list list_n2'})
photo_list = []
for i in text_find_all:
links = i.find('a').get('href')
old_link = links.split('/', maxsplit=1)[-1]
new_link = url + old_link
photo_list.append(new_link)
for i in photo_list:
response = requests.get(url=i)
response.encoding = 'utf-8'
photo_soup = BeautifulSoup(response.text, 'html.parser')
photo_links = photo_soup.find('div', attrs={'class': 'image_div'})
photo = photo_links.find('img').get('src')
picture_split = photo.rsplit('/', maxsplit=1)[-1]
with open(f'../../python爬虫/other网页图片/图片/{picture_split}', mode='wb') as picture:
picture.write(requests.get(photo).content) # 转换成字节,写入文件
print(f"{picture_split}下载完毕")