转Python 代码批量抓取免费高清图片!
Posted zhzhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转Python 代码批量抓取免费高清图片!相关的知识,希望对你有一定的参考价值。
import requests from bs4 import BeautifulSoup import random import time from fake_useragent import UserAgent for page in range(1, 11): fst_url = r‘https://colorhub.me/search?tag=data&page=‘.format(page) UA = UserAgent() fst_response = requests.get(fst_url, headers=‘User-Agent‘: UA.random) fst_soup = BeautifulSoup(fst_response.text, ‘lxml‘) # print(fst_soup.findAll(name=‘div‘)) # exit() sec_urls = [i.find(‘a‘)[‘href‘] for i in fst_soup.findAll(name=‘div‘, attrs=‘class‘: ‘card‘)] pic_names = [i.find(‘a‘)[‘title‘] for i in fst_soup.findAll(name = ‘div‘, attrs=‘class‘:‘card‘)] for sec_url, pic_name in zip(sec_urls, pic_names): UA = UserAgent() ua = UA.random sec_response = requests.get(sec_url, headers=‘User-Agent‘: ua) sec_soup = BeautifulSoup(sec_response.text, ‘lxml‘) pic_url = ‘https:‘+sec_soup.find(‘img‘, ‘class‘: ‘card-img-top‘)[‘src‘] pic_response = requests.get(pic_url, headers=‘User-Agent‘: ua) with open(pic_name+‘.jpg‘, mode=‘wb‘) as fn: fn.write(pic_response.content) print(pic_name) seconds = random.uniform(1,3) time.sleep(seconds) fn.close()
请各位大虾赐教!
以上是关于转Python 代码批量抓取免费高清图片!的主要内容,如果未能解决你的问题,请参考以下文章