pixabay网站图片爬虫
·
# -*- coding:utf-8 -*-
import os
import time
import requests
import urllib
import uuid
from pyquery import PyQuery
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/75.0.3770.142 Safari/537.36"
}
def href_url_download():
# 1.填写要爬取关键词的list.txt
keyword_list = open("list.txt", 'r', encoding='utf-8')
lines = keyword_list.readlines()
keyword_list.close()
for keyword in lines:
keyword = keyword.strip()
print(keyword)
# 2.修改爬取的页数(1,101),默认爬取100页
for pages in range(1, 3):
page = str(pages)
url = "https://pixabay.com/zh/images/search/" + keyword + "/?pagi=" + page # 获取keyword网页
# print(url)
try:
txt = requests.get(url, headers=headers).text # 获取URL及headers
doc = PyQuery(txt)
# print(doc)
urls = doc(".item a img ").items()
for url in urls:
image_url = url.attr("data-lazy")
if image_url is None:
continue
else:
print(image_url)
image_download(keyword, image_url) # 下载"图片"
except requests.exceptions.ConnectionError as e:
print("requests.exceptions.ConnectionError")
def image_download(keyword, image_url):
# 3.图片存储路径
folder_path = "./image/" + keyword + "/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# print(folder_path)
image = keyword + "_" + str(uuid.uuid1()) + ".jpg" # 图片命名
print(image)
# urllib.request.urlretrieve(href3, folder_path + "/" + image)
try:
content = requests.get(image_url, headers=headers)
with open(folder_path + image, "wb") as f:
f.write(content.content)
except urllib.error.HTTPError:
print("Internal Server Error")
except requests.exceptions.ConnectionError as e:
print("requests.exceptions.ConnectionError")
if __name__ == '__main__':
href_url_download()
更多推荐
所有评论(0)