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()

相关文章

elasticsearch python 查询的两种方法

elasticsearch python 查询的两种方法

elasticsearch python 查询的两种方法,具体内容如下所述: from elasticsearch import Elasticsearch es = Elastic...

在Python中处理日期和时间的基本知识点整理汇总

在Python中处理日期和时间的基本知识点整理汇总

 Python程序可以处理多种方式的日期和时间。日期格式之间的转换是一种常见计算机的杂活。 Python的时间和日历模块,能帮助处理日期和时间。 Tick是什么? 时间...

kafka-python 获取topic lag值方式

说真,这个问题看上去很简单,但“得益”与kafka-python神奇的文档,真的不算简单,反正我是搜了半天还看了半天源码。 直接上代码吧 from kafka import Simp...

pygame游戏之旅 添加游戏暂停功能

pygame游戏之旅 添加游戏暂停功能

本文为大家分享了pygame游戏之旅的第13篇,供大家参考,具体内容如下 定义暂停函数: def paused(): largeText = pygame.font.SysFont...

python3实现名片管理系统

基于python3基础课程,编写名片管理系统训练,有利于熟悉python基础代码的使用。 cards_main.py #! /usr/bin/python3 import card...