吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 642|回复: 3
收起左侧

[求助] python 返回值None的问题

[复制链接]
luolovehk 发表于 2022-10-26 12:02
有结果的时候会正常输出,没有结果返回值 None  求助,需要怎么修改才不会返回 None

[Python] 纯文本查看 复制代码
PAGE_URL = "https://data.eastmoney.com/kzz/default.html"
TARGET_URL = "http://datacenter-web.eastmoney.com/api/data/v1/get"
PARAMS = {
    'reportName': "RPT_BOND_CB_LIST",
    'columns': "ALL",
    'quoteColumns': "f2~01~CONVERT_STOCK_CODE~CONVERT_STOCK_PRICE,f235~10~SECURITY_CODE~TRANSFER_PRICE,f236~10~SECURITY_CODE~TRANSFER_VALUE,f2~10~SECURITY_CODE~CURRENT_BOND_PRICE,f237~10~SECURITY_CODE~TRANSFER_PREMIUM_RATIO,f239~10~SECURITY_CODE~RESALE_TRIG_PRICE,f240~10~SECURITY_CODE~REDEEM_TRIG_PRICE,f23~01~CONVERT_STOCK_CODE~PBV_RATIO",
    'source': "WEB",
    'client': "WEB",
    'sortTypes': -1,
    'sortColumns': "PUBLIC_START_DATE",
    'pageSize': 20,
    'pageNumber': 1
}


def log(string):
    for line in string.splitlines():
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        line = "[%s] %s" % (now, line)
        print(line)


def main():
    req = requests.get(TARGET_URL, params=PARAMS)
    js = json.loads(req.text)
    data = js["result"]["data"]
    recently_public_bonds = []
    recently_listing_bonds = []
    today = datetime.date.today()
    # today = datetime.datetime.strptime("2021-11-24 10:00:13", '%Y-%m-%d %H:%M:%S').date()
    dayRel = ["今天"]
    for item in data:
        if item['PUBLIC_START_DATE'] is not None:
            start_date = datetime.datetime.strptime(
                item['PUBLIC_START_DATE'], '%Y-%m-%d %H:%M:%S').date()
            gap = (start_date - today).days
            if gap >= 0 and gap < 1:
                recently_public_bonds.append(
                    Bond.Bond(item["SECURITY_NAME_ABBR"], item["SECURITY_CODE"], item["CONVERT_STOCK_PRICE"], item["TRANSFER_PRICE"], item['PUBLIC_START_DATE'], item['LISTING_DATE']))
        elif item['VALUE_DATE'] is not None:
            start_date = datetime.datetime.strptime(
                item['VALUE_DATE'], '%Y-%m-%d %H:%M:%S').date()
            gap = (start_date - today).days
            if gap == 0:
                recently_public_bonds.append(
                    Bond.Bond(item["SECURITY_NAME_ABBR"], item["SECURITY_CODE"], item["CONVERT_STOCK_PRICE"], item["TRANSFER_PRICE"], item['VALUE_DATE'], item['LISTING_DATE']))
        if item['LISTING_DATE'] is not None:
            list_date = datetime.datetime.strptime(
                item['LISTING_DATE'], '%Y-%m-%d %H:%M:%S').date()
            gap = (list_date - today).days
            if gap == 0:
                recently_listing_bonds.append(
                    Bond.Bond(item["SECURITY_NAME_ABBR"], item["SECURITY_CODE"], item["CONVERT_STOCK_PRICE"], item["TRANSFER_PRICE"], item['PUBLIC_START_DATE'], item['LISTING_DATE']))
    wx_msg = ''
    wx_msg += '【今日发售:】\n\n'
    if len(recently_public_bonds) > 0:
        for bond in recently_public_bonds:
            if bond.public_start_date is not None:
                gap = (bond.public_start_date - today).days
                if gap < len(dayRel):
                    wx_msg += '- %s' % (dayRel[gap])
                    wx_msg += '- %s: 债券代码:%s,债券简称:%s,正股价:%s,转股价:%s, 转股价值:%.2f' % (
                        dayRel[gap],
                        bond.code,
                        bond.name,
                        bond.price if bond.price is not None else "-",
                        bond.swap_price if bond.swap_price is not None else "-",
                        bond.swap_value if bond.swap_price is not None else 0.0)
                if bond.swap_price is not None and bond.swap_value > 90:
                    wx_msg += '  推荐&#9989;'
            else:
                content="bond.public_start_date 不应该为空,请检查代码或API"
                raise RuntimeError("bond.public_start_date 不应该为空,请检查代码或API")
                return content
            wx_msg += '\n\n'
    else:
        wx_msg += '- 无\n\n'

    wx_msg += '【今日上市:】\n\n'
    if len(recently_listing_bonds) > 0:
        for bond in recently_listing_bonds:
            gap = (bond.listing_date - today).days
            if gap < len(dayRel):
                wx_msg += '- %s: 债券代码:% s,债券简称:% s\n\n' % (
                    dayRel[gap], bond.code, bond.name)
    else:
        wx_msg += '- 无\n\n'

    if len(recently_public_bonds) > 0 or len(recently_listing_bonds) > 0:
        wx_msg += '[点击详情查看一览表]%s' % PAGE_URL
        log(wx_msg)
        content=wx_msg
        return content
    else:
        log("今日无可转债发行或上市")

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sbwfnhn 发表于 2022-10-26 16:02
本帖最后由 sbwfnhn 于 2022-10-26 16:10 编辑

没有测试,哪个值返回none?这个问题加个判断跳过不就行了吗
if  a == 'None' or a is none:
    print(返回值为null,跳过)

或者

if  a == 'None' or a is none:
    pass
cshadow 发表于 2022-10-27 06:56
也有可能是 东财 网站的问题  看看 返回为NONE 的 网页是不是跳转到其他页面了
Hazy 发表于 2022-11-7 17:42
python 的函数在没有 return 时, 默认是return None, 你的return部分, 一个是正常输出, 一个是log(), 你只需要在log()之后return 别的就可以了, 空字符串之类的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 05:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表