玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python实现自动提取国家地理每日图片

python 培训

Python urllib2re模块方法写了个实现自动提取国家地理每日一图的小脚本程序。Python源代码贴在下边,希望和大家多多交流,欢迎有更好的想法的建议的朋友们给我留言。

其它Python图片获取相关文章推荐:用Python 获取图片的Base64编码

Python 自动提取图片方法

Python实现自动提取国家地理每日图片代码如下:

import urllib2
import re

# get page html
page = urllib2.urlopen("http://photography.nationalgeographic.com/ngs_pod_ext/searchPOD.jsp?
month=06&day=10&year=2009&page=")

txt = page.read()

#txt2 = page.read()
page.close()

# define a regex to get the img src
imgre = '<img alt="(?P<alt>[^"]*)" src="(?P<src>/staticfiles/NGS/Shared/StaticFiles/Photography/
Images/POD/.+?-ga.jpg)">'

# define a regex to get summary
summaryre = '<div class="summary">\s*<h1 class="podsummary">(?P<podsummary>[^<h>]*)</h1>\s*<p class="credit">
(?P<credit>[^</>]*)</p>\s*<div class="description">(?P<desc>.*?)<div style="float:right'


# get img alt and source

#www.iplaypython.com

m2 = re.search(imgre, txt)
if m2 is not None:
    print "get picture alt is '%s', src is 'http://photography.nationalgeographic.com%s'" % \
          (m2.group("alt"), m2.group("src"))


# get description
m3 = re.search(summaryre, txt, re.I|re.M|re.S)
if m3 is not None:
    print "photo desc: summary is '%s', credit by '%s', desciption is '%s'" % \
          (m3.group("podsummary"), m3.group("credit"), m3.group("desc"))

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2627.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-06 19:46 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部