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

WBOY
Release: 2016-06-06 11:28:58
Original
1465 people have browsed it

Beautiful Photo!: http://www.beautifulphoto.net/

代码如下:


import urllib2
import re

_random_url = r'http://www.beautifulphoto.net/plugin/RndArticle/'
_img_patt = re.compile(r'python获取beautifulphoto随机某图片代码实例')

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)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!