• Python 爬取壁纸图片


    1:打开网址 查看网页结构
    用谷歌浏览器打开链接 F12查看
    由于我使用的pyquery
    可以看到图片的链接 都在img标签的src属性中 我们只要通过pyquery锁定到这个img标签 就可以继续下一步了

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2019/12/26 13:30
    # @Author  : zwb
    # @File    : GetImage.py
    
    
    import requests
    from  pyquery import PyQuery as pq
    import  time
    import  os
    
    headers={
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 '
                     '(KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
    
    }
    
    def start_request(url):
        r=requests.get(url,headers=headers)
        r.encoding='GBK'
        html= r.text
        return  html
    
    def parse(text,i):
        doc=pq(text)
        images=doc('div.list ul li img').items()
        x=0
        for image in images:
            img_url=image.attr('src')
            img=requests.get(img_url,headers=headers).content
            # 图片存放路径,F为盘符
            path = "F:\image\"+str(i)
            if not os.path.exists(path):
                os.makedirs(path)
            path ="F:\image\"+str(i)+"\"+str(x)+".jpg"
            with open(path, 'wb') as f:
                f.write(img)
                time.sleep(1)
                print("正在下载第{}张图片".format(x))
                x+=1
                print("写入完成")
    
    def main():
        #设置爬取的页面数第一页到第9页
        for i in range(1,10):
            if(i==1):
                url="http://www.netbian.com/index.htm"
            else:
                 url="http://www.netbian.com/index_"+str(i)+".htm"
            print(url)
            text=start_request(url)
            parse(text,i)
    
    if __name__ == "__main__":
        main()
  • 相关阅读:
    算法面试题解答(三)
    计算机基础知识问答
    算法面试题解答(五)
    关于POD
    算法面试题解答(四)
    算法面试题解答(一)
    Perfect Interview (序)
    如何调整Dreamhost主机PHP上传尺寸的限制/How to change the maximal size of uploading in your Dreamhost
    Final Fantasy XIII Finished
    Ortholab has been moved to Google Code
  • 原文地址:https://www.cnblogs.com/zhangwenbo0713/p/12101855.html
Copyright © 2020-2023  润新知