使用 wget 從需要設定 cookie 的網站下載 PDF 文件

使用 wget 從需要設定 cookie 的網站下載 PDF 文件

我想訪問報紙網站,然後下載他們的電子紙副本(PDF 格式)。該網站要求我使用我的電子郵件地址和密碼登錄,然後它允許我訪問這些 PDF URL。

我在“設定會話”時遇到問題獲取。當我從瀏覽器登入網站時,它會設定兩個 cookie 值:

[email protected]
Password=12345

我試過:

wget --post-data "[email protected]&Password=12345" http://epaper.abc.com/login.aspx

但是,這只是下載了登入頁面並將其保存在本地。

登入頁面上的 FORM 有兩個欄位:

txtUserID
txtPassword

和單選按鈕像這樣:

<input id="rbtnManchester" type="radio" checked="checked" name="txtpub" value="44">

另一個按鈕:

<input id="rbtnLondon" type="radio" name="txtpub" value="64">

如果我將其發佈到 login.aspx 頁面,我會得到相同的輸出

wget --post-data "[email protected]&txtPassword=12345&txtpub=44" http://epaper.abc.com/login.aspx

如果我做:

--儲存cookies abc_cookies.txt

除了預設內容之外,它似乎沒有任何其他內容。

最後,如果我--debug也這麼做的話,它會說:

...
Set-Cookie: ASP.NET_SessionId=05kphcn4hjmblq45qgnjoe41; path=/; HttpOnly
...
Stored cookie epaper.abc.com -1 (ANY) / <session> <insecure> [expiry none] ASP.NET_SessionId 05kphcn4hjmblq45qgnjoe41
Length: 107253 (105K) [text/html]
Saving to: `login.aspx'
...
Saving cookies to abc_cookies.txt.

但是,abc_cookies.txt 僅顯示以下內容:

# HTTP cookie file.
# Generated by Wget on 2011-08-16 08:03:05.
# Edit at your own risk.

(我不確定為什麼我在 Stack Overflow 上沒有得到任何回應 - 也許超級用戶是一個更好的網站 -使用 Wget 從需要設定 cookie 的網站下載 PDF 文件.)


編輯1

C:\Temp>wget --cookies=on --keep-session-cookies --save-cookies abc_cookies.txt --post-data "txtUserID=abc%40gmail.com&txtPassword=password&txtpub=44&chkbox=checkbox&submit.x=48&submit.y=7" http://epaper.abc.com/login.aspx --debug
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc
DEBUG output created by Wget 1.11.4 on Windows-MinGW.

--2011-08-18 08:15:59--  http://epaper.abc.com/login.aspx
Resolving epaper.abc.com... seconds 0.00, 999.999.99.99
Caching epaper.abc.com => 999.999.99.99
Connecting to epaper.abc.com|999.999.99.99|:80... seconds 0.00, connected.
Created socket 300.
Releasing 0x00a2ae80 (new refcount 1).

---request begin---
POST /login.aspx HTTP/1.0
User-Agent: Wget/1.11.4
Accept: */*
Host: epaper.abc.com
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 100

---request end---
[POST data: txtUserID=abc%40gmail.com&txtPassword=password&txtpub=44&chkbox=checkbox&submit.x=48&submit.y=7]
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 200 OK
Connection: keep-alive
Date: Thu, 18 Aug 2011 02:46:17 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=owcrje55yl45kgmhn43gq145; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 107253

---response end---
200 OK
Registered socket 300 for persistent reuse.

Stored cookie epaper.abc.com -1 (ANY) / <session> <insecure> [expiry none] ASP.NET_SessionId owcrje55yl45kgmhn43gq145
Length: 107253 (105K) [text/html]
Saving to: `login.aspx.1'

100%[======================================================================================================================>] 107,253     24.9K/s   in 4.2s

2011-08-18 08:16:05 (24.9 KB/s) - `login.aspx.1' saved [107253/107253]

Saving cookies to abc_cookies.txt.
Done saving cookies.

C:\Temp>wget --referer=http://epaper.abc.com/login.aspx --cookies=on --load-cookies abc_cookies.txt --keep-session-cookies --save-cookies abc_cookies.txt http://epaper.abc.com/PagePrint/16_08_2011_001.pdf --debug
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc
DEBUG output created by Wget 1.11.4 on Windows-MinGW.


Stored cookie epaper.abc.com -1 (ANY) / <session> <insecure> [expiry none] ASP.NET_SessionId owcrje55yl45kgmhn43gq145
--2011-08-18 08:16:12--  http://epaper.abc.com/PagePrint/16_08_2011_001.pdf
Resolving epaper.abc.com... seconds 0.00, 999.999.99.99
Caching epaper.abc.com => 999.999.99.99
Connecting to epaper.abc.com|999.999.99.99|:80... seconds 0.00, connected.
Created socket 300.
Releasing 0x00598290 (new refcount 1).

---request begin---
GET /PagePrint/16_08_2011_001.pdf HTTP/1.0
Referer: http://epaper.abc.com/login.aspx
User-Agent: Wget/1.11.4
Accept: */*
Host: epaper.abc.com
Connection: Keep-Alive
Cookie: ASP.NET_SessionId=owcrje55yl45kgmhn43gq145

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 200 OK
Connection: keep-alive
Date: Thu, 18 Aug 2011 02:46:30 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
content-disposition: attachement; filename=Default_logo.gif
Cache-Control: private
Content-Type: image/GIF
Content-Length: 4568

---response end---
200 OK
Registered socket 300 for persistent reuse.
Length: 4568 (4.5K) [image/GIF]
Saving to: `16_08_2011_001.pdf'

100%[======================================================================================================================>] 4,568       7.74K/s   in 0.6s

2011-08-18 08:16:14 (7.74 KB/s) - `16_08_2011_001.pdf' saved [4568/4568]

Saving cookies to abc_cookies.txt.
Done saving cookies.

abc_cookies.txt 的內容

epaper.abc.com       FALSE   /       FALSE   0       ASP.NET_SessionId       owcrje55yl45kgmhn43gq145

答案1

我認為你需要使用--keep-session-cookies來保存會話cookie,而不僅僅是--save-cookies(你需要兩者)。

基本上,你

wget --keep-session-cookies --save-cookies ..... url

登入並取得您的會話 cookie。

然後

wget --load-cookie ...... url

下載 PDF。

答案2

也許這會有所幫助。我嘗試登入的網站有一些隱藏字段,我需要先取得這些字段才能成功登入。因此,第一個 wget 獲取登錄頁面以查找額外字段,第二個 wget 登錄站點並保存 cookie,第三個 wget 然後使用這些 cookie 獲取您想要的頁面。

#!/bin/bash

# get the login page to get the hidden field data
wget -a log.txt -O loginpage.html http://foobar/default.aspx
hiddendata=`cat loginpage.html | grep value | grep foobarhidden | tr '=' ' ' | awk '{print $9}' | sed s/\"//g`
rm loginpage.html

# login into the page and save the cookies
postData=user=fakeuser'&'pw=password'&'foobarhidden=${hiddendata}
wget -a log.txt -O /dev/null --post-data ${postData} --keep-session-cookies --save-cookies cookies.txt http://foobar/default.aspx

# get the page you're after
wget -a log.txt -O results.html --load-cookies cookies.txt http://foobar/lister.aspx?id=42
rm cookies.txt

有一些有用的信息這個其他所以貼文:

相關內容