-1

我正在使用下面的代码从网站上抓取数据,其中三个字段由用户输入填充。

import requests 
import json
  

URL = "http://example.com"
  
docs = input("Doc type: ")
fromdate = input("From: ")
todate = input("To: ")

r = requests.post(url = URL, json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":'"{}"',"FromDate":'"{}"',"ToDate":'"{}"'.format(docs,fromdate,todate)})
r.json()

但我收到这样的错误

{'exceptionMessage':'输入字符串格式不正确。','exceptionType':'System.FormatException','message':'发生错误。','stackTrace':'在System.Number.StringToNumber (String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n 在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)\r\n 在 BrowserView.Models.SearchCriteria.Parse() \r\n 在 BrowserView.Controllers.SearchController.SearchTwo(SearchCriteria 条件)'}

我想要下面给出的代码

json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":'"{}"',"FromDate":'"{}"',"ToDate":'"{}"'.format(docs,fromdate,todate)}

当用户输入数据时是这样的

json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":"TAX","FromDate":"20201104","ToDate":"20201218"}
4

2 回答 2

2

这个怎么样:

r = requests.post(url=URL, json= 
    {
      "MaxRows": 0,
      "RowsPerPage": 0,
      "StartRow": 0,
      "DocTypes": docs,
      "FromDate": fromdate,
      "ToDate": todate
     })

input()将每个值作为字符串返回,不需要格式字符串。

于 2020-12-31T20:32:21.993 回答
2

如果问题仅在于将变量插入到您的 json 中,请尝试以下操作:

r = requests.post(url=URL, json={"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":f'{docs}',"FromDate":f'{fromdate}',"ToDate":f'{todate}'})
于 2020-12-31T20:32:22.483 回答