我有一個 bash 腳本,它從一個 URL 下載大量檔案并將它們保存到我的電腦中,例如:
curl 'http://website.com/shop.php?store_id=[66-900]&page=[7-444]&PHPSESSID=phpsessid' -O
(以及一組標頭設定,這是您在 firefox 中將請求復制為 curl 時得到的典型請求)
試圖在 python 中重寫腳本,但事實證明它非常困難(完全新手),例如:
import requests
cookies = {
'PHPSESSID': '<phpsessid here>',
}
headers = {
'<bunch of header stuff here>'
}
params = (
('store_id', '[66-900]'),
('page', '[7-444]'),
('PHPSESSID', '<phpsessid here>'),
)
response = requests.get('http://website.com/shop.php', headers=headers, params=params, cookies=cookies)
但它什么也沒做(curl 命令按照命令下載所有內容)我意識到我可能需要將下載的內容寫入檔案但完全丟失了,因為我處理的不是一個檔案而是數百個檔案,并且從 curl 中可以看出上面的命令。
完全丟失了我所缺少的或需要做的以在 python 中復制這樣的 curl 請求,任何幫助表示贊賞!
uj5u.com熱心網友回復:
您不能像使用 curl 那樣使用范圍。
正如@furas 所指出的,您可以使用回圈,因為 curl 會發送包含范圍內所有引陣列合的請求。
函式response.get回傳類的物件requests.models.Response,因此您應該使用json()方法或屬性content(回傳位元組)和text(回傳字串)。
uj5u.com熱心網友回復:
你必須使用for-loops 而不是[66-900],[7-444]
for store_id in range(66, 990 1):
for page in range(7, 444 1):
params = {
'store_id': store_id,
'page': page,
'PHPSESSID': '<phpsessid here>',
}
response = requests.get(...)
with open(f"{store_id}-{page}", "wb") as fh:
fh.write(response.content)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351727.html
上一篇:基于有效值的組合聚合
下一篇:根據名稱組合兩個元組串列
