python获取糗百图片代码实例

yipeiwu_com5年前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工程师面试必备25条知识点

Python工程师面试必备25条Python知识点: 1.到底什么是Python?你可以在回答中与其他技术进行对比 下面是一些关键点: Python是一种解释型语言。这就是说,与C语言和...

pydev使用wxpython找不到路径的解决方法

问题: pydev使用wx库开发的过程中,import时碰到wx可以识别,但是其它很多函数和变量上面全部是红叉,即无法识别。 解决方法: 1、window->preferences...

Python聚类算法之DBSACN实例分析

Python聚类算法之DBSACN实例分析

本文实例讲述了Python聚类算法之DBSACN。分享给大家供大家参考,具体如下: DBSCAN:是一种简单的,基于密度的聚类算法。本次实现中,DBSCAN使用了基于中心的方法。在基于中...

python实现ping的方法

本文实例讲述了python实现ping的方法。分享给大家供大家参考。具体如下: #!/usr/bin/env python #coding:utf-8 import os, sys,...

在Python的Django框架中simple-todo工具的简单使用

缘起 simple-todo最早是web.py一个中文教程的例子。后来Uliweb的作者limodou 认为这个教程很不错,于是有了Uliweb版的simple-todo。接着又有了Bo...