python爬虫项目-优美图库
如果你是python爬虫新手,想要了解、熟悉爬虫的原理。那么这个优美图库爬取图片将会是和不错的入门项目选择。
用到技术点:
1.requests发送请求,从服务器获取数据
2.BeabtifulSoup 来解析整个页面的源代码--> 特简单
首先我们来看一下效果图
下面附上代码
# 爬取网站第一件事:发送请求到服务器
resp = requests.get("https://www.youmeitu.com/meinv/")
resp.encoding = 'utf-8'
# 解析html
main_page = BeautifulSoup(resp.text, "html.parser")
# 从页面中找到某些东西
# find() 找一个
# find_all() 找全部
aList = main_page.find("div", attrs={"class": "TypeList"}).find_all("a", attrs={"class": "TypeBigPics"})
n = 1
for a in aList:
# 发送请求到子页面,进入到有小姐姐的页面中
href = a.get("href") # /meinv/139741.html
resp1 = requests.get("https://www.youmeitu.com/" + href)
resp1.encoding = 'utf-8'
child_page = BeautifulSoup(resp1.text, "html.parser")
# 找到图片真实路径
src = child_page.find("div", attrs={"class": "ImageBody"}).find("img").get("src")
# 发送请求到服务器,把图片保存在本地
# 创建文件
f = open("图片_%d.jpg" % n, mode="wb") # wb表示写入的内容是非文本文件
f.write(requests.get("https://www.youmeitu.com/" + src).content) # content向外拿出图片的数据,不是文本信息)
print("恭喜恭喜,下载了%d张小姐姐图片" % n)
n += 1
*************************************************************************
您的建议是博主更新最大的动力!!
如发现错误请在评论区评论,博主会仔细查看并修改的!!
希望对您有所帮助!!!