python-requests 没有抓取 JSESSIONID 和 SessionData cookies
python-requests does not grab JSESSIONID and SessionData cookies
我想从 http://www.jstor.org/stable/pdf/10.1086/512825.pdf 抓取一个 pdf 文件,但它要我接受条款和条件。从浏览器下载时,我发现 JSTOR 将我的接受保存在 2 个名为 JSESSIONID 和 SessionData 的 cookie 中,但是 python-requests 不获取这两个 cookie(它获取另外两个 cookie 但不是这些)。
这是我的会话实例化代码:
def get_raw_session():
session = requests.Session()
session.headers.update({'User-Agent': UserAgent().random})
session.headers.update({'Connection': 'keep-alive'})
return session
请注意,我之前多次使用 python-requests for login-required sites 并且效果很好,但在这种情况下却不是。
我想问题是 JSTOR 是用 jsp 构建的,而 python-requests 不支持它。
有什么想法吗?
以下代码对我来说工作得很好 -
import requests
from bs4 import BeautifulSoup
s = requests.session()
r = s.get('http://www.jstor.org/stable/pdf/10.1086/512825.pdf')
soup = BeautifulSoup(r.content)
pdfurl = 'http://www.jstor.org' + soup.find('a', id='acptTC')['href']
with open('export.pdf', 'wb') as handle:
response = s.get(pdfurl, stream=True)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
我想从 http://www.jstor.org/stable/pdf/10.1086/512825.pdf 抓取一个 pdf 文件,但它要我接受条款和条件。从浏览器下载时,我发现 JSTOR 将我的接受保存在 2 个名为 JSESSIONID 和 SessionData 的 cookie 中,但是 python-requests 不获取这两个 cookie(它获取另外两个 cookie 但不是这些)。
这是我的会话实例化代码:
def get_raw_session():
session = requests.Session()
session.headers.update({'User-Agent': UserAgent().random})
session.headers.update({'Connection': 'keep-alive'})
return session
请注意,我之前多次使用 python-requests for login-required sites 并且效果很好,但在这种情况下却不是。
我想问题是 JSTOR 是用 jsp 构建的,而 python-requests 不支持它。
有什么想法吗?
以下代码对我来说工作得很好 -
import requests
from bs4 import BeautifulSoup
s = requests.session()
r = s.get('http://www.jstor.org/stable/pdf/10.1086/512825.pdf')
soup = BeautifulSoup(r.content)
pdfurl = 'http://www.jstor.org' + soup.find('a', id='acptTC')['href']
with open('export.pdf', 'wb') as handle:
response = s.get(pdfurl, stream=True)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)