Python学习笔记:Python网络爬虫,实现爬取网络图片

来源:互联网 发布:东北 校园网络电视 编辑:程序博客网 时间:2024/06/05 03:28

本文主要通过Python爬取百度壁纸的图片

程序代码:

import urllib
import re

def GetHtml(url):
    page = urllib.urlopen(url)
    html = page.read();
    return html

def GetImg(html):
    reg = '"((http|ftp)s?://.*?)"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    x=0
    for imgurl in imglist:
        str=imgurl[0]
        if str.find(".jpg")==-1:
            continue
        try:
            urllib.urlretrieve(str,"%s.jpg"% x)
            x+=1
        except  :
            continue

def ReadHtmlText(urlStr):
    html = urllib.urlopen(urlStr)
    print(html.read())

htmltex=GetHtml("http://image.baidu.com/search/index?ct=201326592&tn=baiduimage&word=%E5%A3%81%E7%BA%B8%20%E4%B8%8D%E5%90%8C%E9%A3%8E%E6%A0%BC%20%E7%BE%8E%E5%A5%B3&pn=0&ie=utf-8&oe=utf-8&cl=2&lm=-1&fr=ala&se=&sme=")
print GetImg(htmltex)

阅读全文
0 0