如何通过Cookie登录亚马逊并爬取Kindle标注内容?
Amazon Kindle提供了查看标注的页面:https://read.amazon.com/notebook。我尝试爬取这些标注但多次失败,不过Readwise和Reflect.app的Chrome扩展能实现该功能,说明操作是可行的。
我找到一个能返回已读书籍列表JSON数据的URL:https://read.amazon.com/kindle-library/search?query=&libraryType=BOOKS&sortType=recency,把浏览器Network标签里的请求复制为cURL导入Postman能成功获取数据,但用前端Fetch、Axios和后端Python Flask发起请求均失败:
前端请求失败(CORS错误)
- 错误提示:
Access to fetch at 'https://read.amazon.com/kindle-library/search?query=&libraryType=BOOKS&sortType=recency' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.
- 我的Fetch代码:
const request = await fetch( "https://read.amazon.com/kindle-library/search?query=&libraryType=BOOKS&sortType=recency", { headers: { accept: "*/*", "accept-language": "en-US,en;q=0.9", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "none", }, referrerPolicy: "strict-origin-when-cross-origin", body: null, method: "GET", mode: "cors", credentials: "include", } );
- 尝试过的无效方案:将mode改为"no-cors"但返回空响应;手动添加会话Cookie到请求头也没用。
后端Python Flask请求失败
- 问题:请求后会直接重定向到亚马逊登录页,即使添加了会话Cookie也无法解决。
- 我的代码:
@api.route('/data') def data(): session = requests.session() request_url = 'https://read.amazon.com/kindle-library/search?query=&libraryType=BOOKS&sortType=recency' headers = { "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "cookie": "<cookie session info>" } params = { "referrerPolicy": "strict-origin-when-cross-origin", } req = session.get(request_url, headers=headers, params=params)
疑问与需求
我明白需要通过Cookie完成亚马逊的身份认证,目前只试过硬编码自己的Cookie,还需要获取用户会话的Cookie,但不清楚问题出在哪,也想了解有没有更优的实现方案。
内容的提问来源于stack exchange,提问作者Julian Alvarez

