from bs4 import BeautifulSoup
import requests
import time
url = 'https://www.umeituku.com/bizhitupian/diannaobizhi/'
resp = requests.get(url)
resp.encoding = 'utf-8'
# print(resp.text)
main_page = BeautifulSoup(resp.text, 'html.parser')
alist = main_page.find('div', class_='TypeList').find_all('a')
# print(alist)
for a in alist:
href = a.get('href')
# 拿到子页面的源代码
chile_page = requests.get(href)
chile_page.encoding = 'utf-8'
chile_page_html = BeautifulSoup(chile_page.text, 'html.parser')
# 定位到div class=ImageBody
div_li = chile_page_html.find('div', class_='ImageBody').find_all('img')
for img in div_li:
img_src = img.get('src')
# print(img_src)
# 下载图片
img_resp = requests.get(img_src)
img_resp.content # 这里拿到的是字节
image_name = img_src.split('/')[-1]
with open(image_name, 'wb') as f:
f.write(img_resp.content) # 写入图片
time.sleep(1) # 增加容错
print("success", image_name)
Python爬虫 学习Bs4 爬取高清图片
最新推荐文章于 2024-10-07 01:36:35 发布