2017-02-06 61 views
0

我正在練習使用python3爬行。Jsessionid干擾爬行

我爬這個網站。

http://www.keri.org/web/www/research_0201?p_p_id=EXT_BBS&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_EXT_BBS_struts_action=%2Fext%2Fbbs%2Fview&_EXT_BBS_sCategory=&_EXT_BBS_sKeyType=&_EXT_BBS_sKeyword=&_EXT_BBS_curPage=1&_EXT_BBS_optKeyType1=&_EXT_BBS_optKeyType2=&_EXT_BBS_optKeyword1=&_EXT_BBS_optKeyword2=&_EXT_BBS_sLayoutId=0 

我想從html代碼中得到pdf的地址。

前)在HTML,PDF下來網址是

http://www.keri.org/web/www/research_0201?p_p_id=EXT_BBS&p_p_lifecycle=1&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_EXT_BBS_struts_action=%2Fext%2Fbbs%2Fget_file&_EXT_BBS_extFileId=5326 

但是,我的履帶結果

http://www.keri.org/web/www/research_0201**;jsessionid=3875698676A3025D8877C4EEBA67D6DF**p_p_id=EXT_BBS&p_p_lifecycle=1&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_EXT_BBS_struts_action=%2Fext%2Fbbs%2Fget_file&_EXT_BBS_extFileId=5306 

我不能將文件下載甚至到下面的地址。

jsessionid從哪裏來?

我可以刪除它,但我不知道爲什麼。

** 爲什麼這麼長的URL?笑

回答

1

我在我的代碼測試了jsessionid劑量不會影響下載文件:

import requests, bs4 

r = requests.get('http://www.keri.org/web/www/research_0201?p_p_id=EXT_BBS&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_EXT_BBS_struts_action=%2Fext%2Fbbs%2Fview&_EXT_BBS_sCategory=&_EXT_BBS_sKeyType=&_EXT_BBS_sKeyword=&_EXT_BBS_curPage=1&_EXT_BBS_optKeyType1=&_EXT_BBS_optKeyType2=&_EXT_BBS_optKeyword1=&_EXT_BBS_optKeyword2=&_EXT_BBS_sLayoutId=0') 
soup = bs4.BeautifulSoup(r.text, 'lxml') 
down_links = [(a.get('href'), a.find_previous('a').text)for a in soup('a', class_="download")] 
for link, title in down_links: 
    filename = title + '.pdf' 
    r = requests.get(link, stream=True, headers=headers) 
    with open(filename, 'wb') as f: 
     for chunk in r.iter_content(chunk_size=1024): 
      f.write(chunk) 
+0

@真的嗎?我直接在網頁瀏覽器中輸入下載網址。但是,我無法下載該文件....無論如何,謝謝〜! – StackQ