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


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

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

Python 自动提取图片方法

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

import urllib2import re# get page htmlpage = 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 srcimgre = '<img alt="(?P<alt>[^"]*)" src="(?P<src>/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/.+?-ga.jpg)">'# define a regex to get summarysummaryre = '<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.iplaypy.comm2 = 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 descriptionm3 = 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"))

编橙之家文章,

评论关闭