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

python之爬虫-必应壁纸

时间:2019-07-20 00:18:48      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:range   https   图片   壁纸   with   url   repo   结果   with open   

python之爬虫-必应壁纸

import re
import requests
"""
@author RansySun
@create 2019-07-19-20:26
"""

"""
https://bing.ioliu.cn/?p=1"
https://bing.ioliu.cn/?p=2"
https://bing.ioliu.cn/?p=3"

"""
count  = 1
for i in range(50):
    url = f"https://bing.ioliu.cn/?p={i}"
    reponse = requests.get(url)
    data = reponse.text
    # 获取必应壁纸图片的链接
    result_list = re.findall('src="(.*?)"', data)
    g = 0
    for result in result_list: 
        # 处理其他链接
        if g < 4:
            g += 1
            continue
        # 判断是否是图片的链接
        if result.startswith("https"):            
            # 请求图片地址           
            img_response = requests.get(result)
            img_data = img_response.content
            # 获取图片名
            img_name = result.split("/")[-1].split("_")[0]+".jpg"
            img_star_name = str(count) + "_" + img_name
           # 写入图片
            with open(img_star_name, "wb") as fw:
                fw.write(img_data)
                print("爬取成功:", img_star_name)
                fw.flush()
            count += 1
            print(result)

结果:
技术图片

技术图片

python之爬虫-必应壁纸

标签:range   https   图片   壁纸   with   url   repo   结果   with open   

原文地址:https://www.cnblogs.com/randysun/p/11216218.html

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