爬虫初学者小练笔1-爬取全站壁纸

import requests from bs4
import BeautifulSoup 
# 寻找图片地址
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 Edg/81.0.416.64'}
for i in range(1,51):
url='[http://simpledesktops.com/browse/]{}/'.format(i)res=requests.get (url,headers=headers)
bs=BeautifulSoup(res.text ,' html.parser ')
pictures_url=bs.find_all('div',class_="desktop")
for j in pictures_url:
pic_url=j.find ('img')['src'][:-17]
pic_name=j.find ('img')['title']
# #下载图片
res=requests.get (pic_url)
pic=res.content
photo = open('{}.jpg'.format(pic_name), 'wb')
photo.write (pic)
photo.close ()

你可能感兴趣的:(笔记)