码迷,mamicode.com
首页 > 编程语言 > 详细

【转】Python 代码批量抓取免费高清图片!

时间:2019-07-24 17:58:44      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:jpg   and   import   sea   for   time   bsp   attr   ref   

 

import requests
from bs4 import BeautifulSoup
import random
import time
from fake_useragent import UserAgent


for page in range(1, 11):
    fst_url = rhttps://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 代码批量抓取免费高清图片!

标签:jpg   and   import   sea   for   time   bsp   attr   ref   

原文地址:https://www.cnblogs.com/zhzhang/p/11239645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!