此程序需要下载这些模块
先创建一个叫作img的文件夹
#1.拿到页面源代码,然后提取到子页面的链接地址,href
#2.通过href拿到子页面内容。从子页面中站到图片的下载地址 img->src
#3.下载图片
import requests
import time
from bs4 import BeautifulSoup
url = "http://www.umeituku.com/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = 'utf-8'
#把源码交给bs
main_page = BeautifulSoup(resp.text,"html.parser")
resp.close()
alist = main_page.find("div",class_="TypeList").find_all("a")
for a in alist:
href = a.get('href') #直接通过get就可以拿到属性值
#拿到页面的源代码
child_page_resp = requests.get(href)
child_page_resp.close()
child_page_resp.encoding = 'utf-8'
#从子页面中拿到图片的下载路径
child_page = BeautifulSoup(child_page_resp.text,'html.parser')
p = child_page.find("div",class_="ImageBody")
img = p.find("img")
src = img.get("src")
#print(img.get("src"))
#下载图片
img_resp = requests.get(src)
#img_resp.content #这里拿到的是字节
img_name = src.split("/")[-1]
with open("img/"+img_name,mode="wb") as f:
f.write(img_resp.content)
print("over!!!",img_name)
time.sleep(1) #防止被服务器封了,禁止访问
print("all over!!!")
爬取的部分结果