python获取糗百图片代码实例

yipeiwu_com6年前Python基础

复制代码 代码如下:

from sgmllib import SGMLParser
import urllib2

class sgm(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.srcs=[]
        self.ISTRUE=True

    def start_div(self,artts):
        for k,v in artts:
            if v=="author":
                self.ISTRUE=False
    def end_div(self):
        self.ISTRUE=True
    def start_img(self,artts):
        for k,v in artts:
            if k=="src" and self.ISTRUE==True:
                self.srcs.append(v)

    def download(self):
        for src in self.srcs:
            f=open(src[-12:],"wb")
            print src
            img=urllib2.urlopen(src)
            f.write(img.read())
            f.close()
sgm=sgm()
for page in range(1,500):
    url="http://www.qiushibaike.com/late/page/%s?s=4622726" % page
    data=urllib2.urlopen(url).read()
    sgm.feed(data)
    sgm.download()

相关文章

Python 3中的yield from语法详解

前言 最近在捣鼓Autobahn,它有给出个例子是基于asyncio 的,想着说放到pypy3上跑跑看竟然就……失败了。 pip install asyncio直接报invalid sy...

Django中提供的6种缓存方式详解

前言 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者m...

python用线性回归预测股票价格的实现代码

python用线性回归预测股票价格的实现代码

线性回归在整个财务中广泛应用于众多应用程序中。在之前的教程中,我们使用普通最小二乘法(OLS)计算了公司的beta与相对索引的比较。现在,我们将使用线性回归来估计股票价格。 线性回归是一...

对pandas中两种数据类型Series和DataFrame的区别详解

对pandas中两种数据类型Series和DataFrame的区别详解

1. Series相当于数组numpy.array类似 s1=pd.Series([1,2,4,6,7,2]) s2=pd.Series([4,3,1,57,8],index=['a...

python获取当前文件路径以及父文件路径的方法

python获取当前文件路径以及父文件路径的方法

#当前文件的路径 pwd = os.getcwd() #当前文件的父路径 father_path=os.path.abspath(os.path.dirname(pwd)+os.p...