作者hiddenwind (我可以重来吗)
看板Python
标题[问题] json爬虫条件自动带入日期
时间Sun Jul 23 21:03:38 2017
请教各位先进
我想抓取特定时间内的市场交易资料,使用语法如下:
因为查询起点(startdate)为固定106.01.01
我想让查询终点(enddate)为每天。
目前只能用手动输入。
import json
import requests
res = requests.get("
http://m.coa.gov.tw/OpenData/FarmTransData.aspx?StartDate=106.01.01&EndDate=106.07.23")
print(res.text)
後来我将python今天日期转换成他的格式yyy.mm.dd
import time
交易日期=time.time()
localtime = time.localtime(交易日期)
print("年: ", localtime[0])
print("月: ", localtime[1])
print("日: ", localtime[2])
print("时: ", localtime[3])
print("分: ", localtime[4])
print("秒: ", localtime[5])
formattime = time.asctime(time.localtime(交易日期))
print(formattime)
m = str(localtime[1])
mm = m.zfill(2)
d = str(localtime[2])
dd = d.zfill(2)
yyymmdd =print(str(localtime[0]-1911)+"."+str(mm)+"."+str(dd))
可以得到106.07.23
但我应该要如何做才能将yyymmdd带入enddate呢?
感谢大家~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 180.218.124.102
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1500815025.A.954.html
1F:→ zerof: ... url="...EndDate=%s" % end_date 07/23 21:15
2F:→ uranusjr: 比较好的方法是用 get() 的 params 参数 07/24 02:33
5F:→ coeric: res=requests.get(url) 07/24 12:19
6F:→ coeric: 我觉得你该有的作法,都有了不是? 07/24 12:19
7F:→ hiddenwind: 谢谢协助,因为我想作的是依要求的搜寻格式作出今日 07/24 19:46
8F:→ hiddenwind: 的日期 07/24 19:46
9F:→ hiddenwind: 再带入相对的条件(ENDDATE),这样後续就能打开自动执 07/24 19:47
10F:→ hiddenwind: 行 07/24 19:47
12F:→ hiddenwind: /OpenData/FarmTransData.aspx?StartDate=106.01.01& 07/24 19:50
13F:→ hiddenwind: EndDate=106.07.24 07/24 19:50
14F:→ hiddenwind: 问题是在enddate上 07/24 19:51
15F:→ hiddenwind: 谢谢U大,这样写比较易读,可是一样无法把yyymmdd带入 07/24 21:27
16F:→ s860134: 从上述语意来看 coeric 说的就是你要的吧 ? 07/25 06:48
17F:→ s860134: 你的问题出在 yyymmdd =print(...) 07/25 06:49
18F:→ s860134: 这 print 本身不会回传任何东西 yyymmdd 赋值为 None 07/25 06:49
19F:→ s860134: 把 print 拿掉,剩下一切都会正常 07/25 06:49
20F:→ s860134: u 大说的方式可以让你的带入方式比较美观好维护 07/25 06:50
21F:→ coeric: 因为既然都有能力print出来 剩下就是带进去而已 应该没啥 07/25 18:22
22F:→ coeric: 在这看文,也是一个精进自己的方法,u大说的方式 07/25 18:26
23F:→ coeric: 我似乎三天两头就会看到,但却没去思考它的美观与方便 07/25 18:27
24F:→ coeric: 日後也该慢慢调整自己coding的模式了 07/25 18:28
25F:→ hiddenwind: 感谢楼上各位大大,成功了!还有很多要跟版上学习orz 07/25 19:59