tianyagd 发表于 2023-11-14 12:36

参数里面的变量该如何写呢?

# 获取毫秒级时间戳,用于网页验证
def getTime():
    return int(round(time.time() * 1000))

code_list=["110000", "120000","130000", "140000", "150000","210000"]

for code in code_list:
       key={}
       key['m']='QueryData'
       key['dbcode']='fsnd'
       key['rowcode']='zb'
       key['colcode']='sj'
       key['wds']='[{"wdcode":"reg","valuecode":"110000"}]'
       key['dfwds']='[{"wdcode":"sj","valuecode":"LAST20"}]'
       key['k1']=str(getTime())


我希望用code来代替"110000",如何写呢?

tianyagd 发表于 2023-11-14 12:41

本帖最后由 tianyagd 于 2023-11-14 12:42 编辑

parameters={

'm':'QueryData',

'dbcode':'fsnd',

'rowcode':'reg',

'colcode':'sj',

'wds':'[{"wdcode":"zb","valuecode":"A020101"}]',

'dfwds':'[{"wdcode":"sj","valuecode":"LAST20"}]',

'k1':'1699793930478 '

}

这样的参数形式,如果用变量zb来替代 "A020101",又如何写呢?谢谢!

头脑发热的少年 发表于 2023-11-14 12:45

def getTime():
    return int(round(time.time() * 1000))


code_list = ["110000", "120000", "130000", "140000", "150000", "210000"]

for code in code_list:
    key = {}
    key['m'] = 'QueryData'
    key['dbcode'] = 'fsnd'
    key['rowcode'] = 'zb'
    key['colcode'] = 'sj'
    key['wds'] = f'[{"wdcode":"reg","valuecode":"{code}"}]'
    key['dfwds'] = '[{"wdcode":"sj","valuecode":"LAST20"}]'
    key['k1'] = str(getTime())

头脑发热的少年 发表于 2023-11-14 12:46

def getTime():
    return int(round(time.time() * 1000))


code_list = ["110000", "120000", "130000", "140000", "150000", "210000"]

for code in code_list:
    key = {}
    key['m'] = 'QueryData'
    key['dbcode'] = 'fsnd'
    key['rowcode'] = 'zb'
    key['colcode'] = 'sj'
    key['wds'] = '[{"wdcode":"reg","valuecode":"' + code + '"}"}]'
    key['dfwds'] = '[{"wdcode":"sj","valuecode":"LAST20"}]'
    key['k1'] = str(getTime())

头脑发热的少年 发表于 2023-11-14 12:47

以上我发的两种方式都可以,其他的类推就行,不难

tianyagd 发表于 2023-11-14 12:50

头脑发热的少年 发表于 2023-11-14 12:45
def getTime():
    return int(round(time.time() * 1000))



ValueError                              Traceback (most recent call last)
Cell In, line 33
   31 key['rowcode']='zb'
   32 key['colcode']='sj'
---> 33 key['wds']=f'[{"wdcode":"reg","valuecode":"{code}"}]'
   34 key['dfwds']='[{"wdcode":"sj","valuecode":"LAST20"}]'
   35 key['k1']=str(getTime())

ValueError: Invalid format specifier

tianyagd 发表于 2023-11-14 12:58

头脑发热的少年 发表于 2023-11-14 12:46
def getTime():
    return int(round(time.time() * 1000))



JSONDecodeError: Expecting value: line 6 column 1 (char 10)

不用变量时,可以正常爬到数据。按照你的方法,错误提示如上

superTian 发表于 2023-11-14 13:49

本帖最后由 superTian 于 2023-11-14 13:51 编辑

# 获取毫秒级时间戳,用于网页验证
def getTime():
    return int(round(time.time() * 1000))

code_list=["110000", "120000","130000", "140000", "150000","210000"]

for code in code_list:
       key={}
       key['m']='QueryData'
       key['dbcode']='fsnd'
       key['rowcode']='zb'
       key['colcode']='sj'
       key['wds']='[{"wdcode":"reg","valuecode":"%s"}]' % code
       key['dfwds']='[{"wdcode":"sj","valuecode":"LAST20"}]'
       key['k1']=str(getTime())

头脑发热的少年 发表于 2023-11-14 15:18

tianyagd 发表于 2023-11-14 12:58
JSONDecodeError: Expecting value: line 6 column 1 (char 10)

不用变量时,可以正常爬到数据。按照 ...

那你就用我的第二种方法,那个方法是肯定可以的,你可以在语句之前打个print,输出一下code,是不是跟你之前的值是一样的

tianyagd 发表于 2023-11-14 19:31

头脑发热的少年 发表于 2023-11-14 12:46
def getTime():
    return int(round(time.time() * 1000))



原来是多了半边花括号
页: [1] 2
查看完整版本: 参数里面的变量该如何写呢?