python获取糗百图片代码实例

WBOY
Release: 2016-06-06 11:29:00
Original
946 people have browsed it

代码如下:


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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!