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

yipeiwu_com5年前Python基础

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 '/zb_users/upload/202003/z3u5xjh4qu4.jpg' % name, '%s.jpg' % name
        return '/zb_users/upload/202003/jf0ocgzt1ev.jpg' % name
    return None

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

相关文章

python-itchat 统计微信群、好友数量,及原始消息数据的实例

python-itchat 统计微信群、好友数量,及原始消息数据的实例

参考来自:https://itchat.readthedocs.io/zh/latest/api/ #coding=utf-8 import itchat from itchat.c...

Python给定一个句子倒序输出单词以及字母的方法

如下所示: #!/usr/bin/python # -*- coding: utf-8 -*- def rever(sentence): newwords = [] word...

python执行shell获取硬件参数写入mysql的方法

本文实例讲述了python执行shell获取硬件参数写入mysql的方法。分享给大家供大家参考。具体分析如下: 最近要获取服务器各种参数,包括cpu、内存、磁盘、型号等信息。试用了Hyp...

python静态方法实例

本文实例讲述了python静态方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:staticmethod Found at: __builtin__ staticme...

Python基本类型的连接组合和互相转换方式(13种)

Python基本类型的连接组合和互相转换方式(13种)

本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的extend()方法和字典中的 update方法非常的常用。 1.连接两个字符串 a = "...