欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python获取beautifulphoto随机某图片代码实例

程序员文章站 2023-10-28 12:23:52
beautiful photo!: http://www.beautifulphoto.net/ 复制代码 代码如下:import urllib2import re _...

beautiful photo!: http://www.beautifulphoto.net/

复制代码 代码如下:

import urllib2
import re

_random_url = r'http://www.beautifulphoto.net/plugin/rndarticle/'
_img_patt = re.compile(r'<img src="http://www\.beautifulphoto\.net/upload/(\d+)\.jpg" />')

def random(timeout=3, more=false):
    try:
        html = urllib2.urlopen(_random_url, timeout=timeout).read()
    except urllib2.urlerror, e:
        return none
    res = re.search(_img_patt, html)
    if res:
        name = res.group(1)
        if more:
            return 'http://www.beautifulphoto.net/upload/', '%s_mini.jpg' % name, '%s.jpg' % name
        return 'http://www.beautifulphoto.net/upload/%s_mini.jpg' % name
    return none

if __name__ == '__main__':
    url = random()
    print(url)
    if url:
        import webbrowser as wb
        wb.open(url)