用python爬虫爬取图片的步骤_python数据爬虫

用python爬虫爬取图片的步骤_python数据爬虫因为平时爱好摄影,所以喜欢看看色影无忌论坛的获奖摄影作品,所以写了个小script用来抓取上面的获奖图片,亲自测试可以使用。#-*-coding:UTF-8-*-#作者RockyChenimportre,urllib,sys,os,time,urllib2,cookielib,stringclassDownload:def__init__(self

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

原文地址: http://www.30daydo.com/article/56

因为平时爱好摄影,所以喜欢看看色影无忌论坛的获奖摄影作品,所以写了个小script用来抓取上面的获奖图片,亲自测试可以使用。

自动抓所有的获奖图片

用python爬虫爬取图片的步骤_python数据爬虫

完整代码: 

#-*-coding=utf-8-*-
__author__ = 'rocchen'
from bs4 import BeautifulSoup
import urllib2,sys,StringIO,gzip,time,random,re,urllib,os
reload(sys)
sys.setdefaultencoding('utf-8')
class Xitek():
    def __init__(self):
        self.url="http://photo.xitek.com/"
        user_agent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        self.headers={"User-Agent":user_agent}
        self.last_page=self.__get_last_page()


    def __get_last_page(self):
        html=self.__getContentAuto(self.url)
        bs=BeautifulSoup(html,"html.parser")
        page=bs.find_all('a',class_="blast")
        last_page=page[0]['href'].split('/')[-1]
        return int(last_page)


    def __getContentAuto(self,url):
        req=urllib2.Request(url,headers=self.headers)
        resp=urllib2.urlopen(req)
        #time.sleep(2*random.random())
        content=resp.read()
        info=resp.info().get("Content-Encoding")
        if info==None:
            return content
        else:
            t=StringIO.StringIO(content)
            gziper=gzip.GzipFile(fileobj=t)
            html = gziper.read()
            return html

    #def __getFileName(self,stream):


    def __download(self,url):
        p=re.compile(r'href="(/photoid/\d+)"')
        #html=self.__getContentNoZip(url)

        html=self.__getContentAuto(url)

        content = p.findall(html)
        for i in content:
            print i

            photoid=self.__getContentAuto(self.url+i)
            bs=BeautifulSoup(photoid,"html.parser")
            final_link=bs.find('img',class_="mimg")['src']
            print final_link
            #pic_stream=self.__getContentAuto(final_link)
            title=bs.title.string.strip()
            filename = re.sub('[\/:*?"<>|]', '-', title)
            filename=filename+'.jpg'
            urllib.urlretrieve(final_link,filename)
            #f=open(filename,'w')
            #f.write(pic_stream)
            #f.close()
        #print html
        #bs=BeautifulSoup(html,"html.parser")
        #content=bs.find_all(p)
        #for i in content:
        #    print i
        '''
        print bs.title
        element_link=bs.find_all('div',class_="element")
        print len(element_link)
        k=1
        for href in element_link:

            #print type(href)
            #print href.tag
        '''
        '''
            if href.children[0]:
                print href.children[0]
        '''
        '''
            t=0

            for i in href.children:
                #if i.a:
                if t==0:
                    #print k
                    if i['href']
                    print link

                        if p.findall(link):
                            full_path=self.url[0:len(self.url)-1]+link
                            sub_html=self.__getContent(full_path)
                            bs=BeautifulSoup(sub_html,"html.parser")
                            final_link=bs.find('img',class_="mimg")['src']
                            #time.sleep(2*random.random())
                            print final_link
                    #k=k+1
                #print type(i)
                #print i.tag
                #if hasattr(i,"href"):
                    #print i['href']
                #print i.tag
                t=t+1
                #print "*"

        '''

        '''
            if href:
                if href.children:
                    print href.children[0]
        '''
            #print "one element link"



    def getPhoto(self):

        start=0
        #use style/0
        photo_url="http://photo.xitek.com/style/0/p/"
        for i in range(start,self.last_page+1):
            url=photo_url+str(i)
            print url
            #time.sleep(1)
            self.__download(url)

        '''
        url="http://photo.xitek.com/style/0/p/10"
        self.__download(url)
        '''
        #url="http://photo.xitek.com/style/0/p/0"
        #html=self.__getContent(url)
        #url="http://photo.xitek.com/"
        #html=self.__getContentNoZip(url)
        #print html
        #'''
def main():
    sub_folder = os.path.join(os.getcwd(), "content")
    if not os.path.exists(sub_folder):
        os.mkdir(sub_folder)
    os.chdir(sub_folder)
    obj=Xitek()
    obj.getPhoto()


if __name__=="__main__":
    main()

详细讲解请移步: 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/193821.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Python漫画下载器「建议收藏」

    Python漫画下载器「建议收藏」“并不是所有数据都应该被获取。”——弗罗指令,53.12很多漫画找不到下载资源了(可能被亚空间吞没了吧),无奈只能从漫画网站上扒了。但一页一页右键保存太浪费资源与时间了,所以写了个Py爬虫扒图片吧~~赞美虚空龙,额,我是说万机神~#coding:utf-8#!/usr/bin/envpython3#=========================================================##漫画下载##================..

  • Docker安装Mysql8和sqlyong连接报错2058的解决方法[随笔记录]

    Docker安装Mysql8和sqlyong连接报错2058的解决方法[随笔记录]

  • MySQL数据库管理工具_mysql数据库管理工具有哪些

    MySQL数据库管理工具_mysql数据库管理工具有哪些SQLyog管理工具MySQL管理工具通过SQLyog连接MySQL使用SQlyog工具创建数据库MySQL管理工具除了使用命令行来操作MySQL数据库之外,还可以使用图形化管理工具来管理数据。SQLyog是一款简洁高效,功能强大的MySQL数据库管理工具。使用SQLyog可以快速直观地让用户完成对数据库的操作。该工具可从SQLyog官网(http://www.webyog.com/cn/)下载。SQLyog安装过程比较容易,这里不多说。通过SQLyog连接MySQL启动MySQL服务后

  • C语言:求两个数的最大公约数和最小公倍数

    C语言:求两个数的最大公约数和最小公倍数C语言:求两个数的最大公约数和最小公倍数求两个数的最大公约数:“辗转相除法”:设两数为a和b(a>b),用a除以b,得a÷b=商…余数,若余数为0,则最大公约数为b;若余数不为0,则再用b÷余数,得b÷余数=商1…余数1,若余数1=0,则最大公约数为余数,若余数1不为0,继续让商÷余数n,一直到能够余数为零这时的除数即最大公约数。求两个数的最小公倍数:最小公倍数=两数的乘积÷…

  • hive中的数据类型:基本数据类型,集合数据类型_hive数据库类型

    hive中的数据类型:基本数据类型,集合数据类型_hive数据库类型数据类型1.Hive中的数据类型分为两类:基本类型和复杂类型2.基本类型包含:tinyint,smallint,int,bigint,float,double,boolean,string,timestamp,binary3.复杂类型:array,map和structa.array:数组类型,对应了Java中的集合或者数组。原始数据jack,johnlucy,miketom,bob,cindylily,helen,mary,alexfrank,grace,iran,edentony

  • 在ubuntu中pycharm2021.12.13最新激活码-激活码分享

    (在ubuntu中pycharm2021.12.13最新激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.cn/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~41MD9IQHZL-eyJsaWNlbnNlSWQiOi…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号