首页 > 解决方案 > 如何使用 Python 登录亚马逊的 Audible.com 子公司

问题描述

我想使用 Python Beautiful Soup 抓取 Audible 网站。除非我登录我的 Audible 帐户,否则我无法访问某些数据。它是亚马逊的子公司。我一直没有成功。我只想使用 Python 登录并抓取 html。

我尝试了各种代码,例如如何使用 BeautifulSoup 登录亚马逊。有人会认为只需在此代码中替换我的凭据即可。

标签: python-3.xpython-requests

解决方案


不幸的是,这不能再在 Python 中简单地自动化。这是我用Audible AU所能得到的。POST 需要一堆标题,其中大部分都可以被提取,除了metadata1(更多在底部):

"""load packages"""
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlsplit, parse_qs

"""define URL where login form is located"""
site = "https://www.audible.com.au/signin"

"""initiate session"""
session = requests.Session()

"""define session headers"""
session.headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9,cs;q=0.8",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
    "metadata1": "",
}

"""get login page"""
resp = session.get(site)
html = resp.text

"""extract clientContext from the login page"""
query = urlsplit(resp.url).query
params = parse_qs(query)
clientContext = params["clientContext"]
new_login_url = "https://www.amazon.com.au/ap/signin/" + str(clientContext[0])

"""get BeautifulSoup object of the html of the login page"""
soup = BeautifulSoup(html, "lxml")

"""scrape login page to get all the needed inputs required for login"""
data = {}
form = soup.find("form", {"name": "signIn"})
for field in form.find_all("input"):
    try:
        data[field["name"]] = field["value"]
    except:
        pass

"""add username and password to the data for post request"""
data[u"email"] = "EMAIL"
data[u"password"] = "PASSWORD"

"""display: redirect URL, appActionToken, appAction, siteState, openid.return_to, prevRID, workflowState, create, email, password"""
print(new_login_url, data)

"""submit post request with username / password and other needed info"""
post_resp = session.post(new_login_url, data=data, allow_redirects=True)
post_soup = BeautifulSoup(post_resp.content, "lxml")

"""check the captcha"""
warning = post_soup.find("div", id="auth-warning-message-box")
if warning:
    print("Warning:", warning)
else: print(post_soup)

session.close()

在线添加您的电子邮件地址和密码4849. 还可以使用浏览器登录并检查流量以查看metadata1计算机上的内容并在线添加22。如果你很幸运并且你不会被检测为机器人,你会进入,否则你会得到验证码图像。

metadata1是 base64 中的大量有效负载,其中包含由您的浏览器收集的数据,这些数据可以唯一地识别您并将您与机器人区分开来(鼠标单击、键入延迟、页面脚本、浏览器信息和兼容性和扩展、Flash 版本、用户代理、脚本性能、硬件 - GPU、本地存储、画布大小等...)


推荐阅读