Python之简单爬取图片并保存到本地文件夹。
程序员文章站
2022-05-09 15:06:50
...
Python爬取图片项目介绍
- 网址地址:https://500px.com/popular
- 第三方库:requests,fake_useragent(产生随机请求头进行访问)
- 代码实现:
import requests
from fake_useragent import UserAgent
filename=0
class photo_spider(object):
def __init__(self):
self.url = 'https://api.500px.com/v1/photos?rpp=50&feature=popular&image_size%5B%5D=1&image_size%5B%5D=2&image_size%5B%5D=32&image_size%5B%5D=31&image_size%5B%5D=33&image_size%5B%5D=34&image_size%5B%5D=35&image_size%5B%5D=36&image_size%5B%5D=2048&image_size%5B%5D=4&image_size%5B%5D=14&sort=&include_states=true&include_licensing=true&formats=jpeg%2Clytro&only=&exclude=&personalized_categories=&page={}&rpp=50'
ua = UserAgent(verify_ssl=False)
# 产生随机的UserAgent请求头进行访问。
for i in range(1, 100):
self.headers = {
'User-Agent': ua.random
}
#发送请求,获取网页。
def get_html(self,url):
response=requests.get(url,headers=self.headers)
html = response.json()
return html
#获取图片链接地址,保存图片格式到本地文件夹。
def get_imageUrl(self,html):
global filename
content_list=html['photos']
for content in content_list:
image_url = content['image_url']
# print(image_url[8])
imageUrl = image_url[8]
r = requests.get(imageUrl, headers=self.headers)
with open('D:/pycharm文件/photo/' + str(filename) + '.jpg', 'wb') as f:
f.write(r.content)
filename += 1
#获取多页及函数调用
def main(self):
start = int(input('输入开始:'))
end = int(input('输入结束页:'))
for page in range(start, end + 1):
print('第%s页' % page)
url = self.url.format(page)
html = self.get_html(url)
self.get_imageUrl(html)
if __name__ == '__main__':
spider = photo_spider()
spider.main()