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使用 Beanstalkd 做异步任务处理的方法

Python使用 Beanstalkd 做异步任务处理的方法

使用 Beanstalkd 作为消息队列服务,然后结合 Python 的装饰器语法实现一个简单的异步任务处理工具. 最终效果 定义任务: from xxxxx.job_queue i...

itchat接口使用示例

有关itchat接口的知识,小编是初步学习,这里先给大家分享一段代码用法示例。 sudo pip3 install itchat 今天用了下itchat接口,从url=”https://...

python中的for循环

python中的for循环

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。 语法: for循环的语法格式如下: for iterating_var in sequence:...

利用python实现.dcm格式图像转为.jpg格式

如下所示: import pydicom import matplotlib.pyplot as plt import scipy.misc import pandas as...

实例讲解Python脚本成为Windows中运行的exe文件

将程序转换为exe文件 我们先来介绍如何使用工具Pyinstaller 安装Pyinstaller 我们用pip安装Pyinstaller 。 注意,如果使用Pyinstaller,则应...