Python通过解析网页实现看报程序的方法

时间:2021-05-22

本文所述实例可以实现基于Python的查看图片报纸《参考消息》并将当天的图片报纸自动下载到本地供查看的功能,具体实现代码如下:

# coding=gbkimport urllib2import socketimport reimport timeimport os# timeout in seconds#timeout = 10#socket.setdefaulttimeout(timeout)timeout = 10urllib2.socket.setdefaulttimeout(timeout)home_url = "http://pile(reg_str) image_results = image_reg.findall(news_img_page) if len(image_results) == 0: print "Cannot find news page" + str(page_index) + "!" download_suc = False break image_url = image_results[0] print "News image url = " + image_url news_image_context = urllib2.urlopen(image_url) image_name = image_folder + "page_" + str(page_index) + ".jpg" imgf = open(image_name, 'wb') print "Getting image..." try: while True: date = news_image_context.read(1024*10) if not date: break imgf.write(date) imgf.close() except: download_suc = False print "Save image " + str(page_index) + " failed!" print "Unexpected error: " + sys.exc_info()[0] + sys.exc_info()[1] else: print "Save image " + str(page_index) + " succeed!" print page_index = page_index + 1if download_suc == True: print "News download succeed! Path = \"" + str(image_folder) + "\"" print "Enjoy it! ^^"else: print "news download failed!"

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章