作者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/m.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