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的命名规则知识点总结

python命名规则 命名风格 python几种不同命名风格 驼峰式命名法(WjW) 混合式命名法(wjWj) 大写(WJWJWJ)或大写加下划线(WJWJWJ) 前缀(wjing)或后...

Python解决pip install时出现的Could not fetch URL问题

Python解决pip install时出现的Could not fetch URL问题

前言 使用python直接使用pip install xx时,出现 Could not fetch URL https://pypi.python.org/simple/requests...

使用Python进行中文繁简转换的实现代码

中文繁体、简体的差异,在NPL中类似英文中的大小写,但又比大小写更为复杂,比如同样为繁体字,大陆、香港和台湾又不一样。先前写过一篇中文繁简转换的文章,感觉写的不太详细,今天就针对Pyth...

python读文件逐行处理的示例代码分享

复制代码 代码如下:import os ## for os.path.isfile() def dealline(line) :    print(line...

Python数据类型详解(四)字典:dict

一.基本数据类型   整数:int   字符串:str(注:\t等于一个tab键)   布尔值: bool   列表:list   列表用[]   元祖:tuple   元祖用()...