作者BoSSTSuNG (BoSSTsunG)
看板Python
标题[问题] payload里的for回圈怎麽写?
时间Thu Aug 10 23:43:53 2017
小弟我python萌新程式如下
res = requests.post("
http://mops.twse.com.tw/mops/web/ajax_t164sb04", data =
payload)
payload = {'encodeURIComponent':'1',
'step':'1',
'firstin':'1',
'off':'1',
'keyword4':'',
'code1':'',
'TYPEK2':'',
'checkbtn':'',
'queryName':'co_id',
'inpuType':'co_id',
'TYPEK':'all',
'isnew':'true',
'co_id':'1264',
'year':'',
'season':''}
print(res.text)
目前的程式码事得到最新一次的资料
我想让year自动跑2014.15.16三年
season跑1.2.3.4
我试过 'season':'p'
p = 1
while p < 5:
p+=1
好像不行故前来求助
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.200.210.89
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1502379836.A.005.html
1F:→ bruce0209: 要2014~2016? 那p怎麽是从1开始? 08/10 23:53
2F:→ bruce0209: 你是要从哪边抓资料? 试着先抓单年单季的看看吧? 08/10 23:54
※ 编辑: BoSSTSuNG (1.200.210.89), 08/10/2017 23:59:49
3F:→ BoSSTSuNG: year已修正,我抓某一加公司某一年的有成功 08/11 00:02
4F:→ bruce0209: for p in range(1,5) 好像是这样吧 08/11 00:05
5F:→ bruce0209: 我个人是觉得资讯太少不知道怎麽帮你修... 08/11 00:08
抱歉我全附上,for p in range(1,5)是要整个放进payload里吗?
※ 编辑: BoSSTSuNG (1.200.210.89), 08/11/2017 00:28:38
6F:→ bruce0209: 你的"好像不行"指的是? 我刚刚试抓好像不能抓旧资料阿? 08/11 00:57
7F:→ bruce0209: 换个地方抓看看? 08/11 00:57
8F:→ bruce0209: 可以了....你的参数isnew:true抓不到旧资料 08/11 01:03
9F:→ bruce0209: 我是这样写 08/11 01:06
10F:→ bruce0209: for year in range(103,105): 08/11 01:07
11F:→ bruce0209: for season in range(1,5): 08/11 01:07
12F:→ bruce0209: postAttrs = self.PostAttrs(year,season) 08/11 01:07
13F:→ bruce0209: response = s.post(url,postAttrs) 08/11 01:08
谢谢耐心指导,请问你的self就是payload对吧,但出现AttributeError: 'dict' object has no attribute 'PostAttrs',所以我想问一下你有import其他套件吗?
※ 编辑: BoSSTSuNG (1.200.210.89), 08/11/2017 14:19:56
14F:→ bruce0209: 没....我是写在class里面所以前面要self呼叫自己的fun 08/11 15:05
15F:→ bruce0209: 然後就只有import requests 08/11 15:07
16F:→ bruce0209: 我的PostAttrs会回传你的payload内容 08/11 15:08