Home  >  Article  >  Backend Development  >  Python获取网页上图片下载地址的方法

Python获取网页上图片下载地址的方法

WBOY
WBOYOriginal
2016-06-06 11:22:521535browse

本文实例讲述了Python获取网页上图片下载地址的方法。分享给大家供大家参考。具体如下:

这里获取网页上图片的下载地址是正在写的数据采集中的一段,代码如下:

代码如下:

#!/user/bin/python3
import urllib2
from HTMLParser import HTMLParser
class MyHtmlParser(HTMLParser):
    links = []
    def handle_starttag(self, tag, attrs):
        if tag == "img":
            if len(attrs) == 0:
                pass
            else:
                for name, value in attrs:
                    if name == "src":
                        self.links.append(value)
if __name__ == "__main__":
    uri = "http://dy.163.com/v2/article/T1374483113516/AGSNE9L000964K4O"
    file = urllib2.urlopen(uri).read()
    # file = "

Title

I'm a paragraph!

"
    hy = MyHtmlParser()
    hy.feed(file)
    hy.close()
    print(hy.links)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
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