【问题标题】:Scraping data from a XHR with request使用请求从 XHR 中抓取数据
【发布时间】:2019-10-28 00:23:14
【问题描述】:

我想抓取数据of this website。但是我得到的结果与网站上发布的结果不同。例如,当我运行代码时,对于 14000 和 48 个月的持续时间,我得到 TAE 的 7.03,而网站上的值为 6.44。我认为参数设置错误。有人可以帮我吗?

我以多种方式更改了参数,但它没有工作。我不知道如何找到正确的参数。

import requests
from bs4 import BeautifulSoup
import re
import json
import pandas as pd

#Let's first collect few auth vars
r = requests.Session()
response = r.get("https://simuladores.bancosantander.es/SantanderES/loansimulatorweb.aspx?por=webpublica&prv=publico&m=100&cta=1&ls=0#/t0")
soup = BeautifulSoup(response.content, 'html')
key = soup.find_all('script',text=re.compile('Afi.AfiAuth.Init'))
pattern = r"Afi.AfiAuth.Init\((.*?)\)"

WSSignature = re.findall(pattern,key[0].text)[0].split(',')[-1].replace('\'','')
WSDateTime = re.findall(pattern,key[0].text)[0].split(',')[1].replace('\'','')

headers = {
    'Origin': 'https://simuladores.bancosantander.es',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
    'Content-Type': 'application/json;charset=UTF-8',
    'Accept': 'application/json, text/plain, */*',
    'WSSignature': WSSignature,
    'Referer': 'https://simuladores.bancosantander.es/SantanderES/loansimulatorweb.aspx?por=webpublica&prv=publico&m=100&cta=1&ls=0',
    'WSDateTime': WSDateTime,
    'WSClientCode': 'SantanderES',
}

#Those are the standard params of a request
params = {'wsInputs': {'finality': 'prestamo coche',
  'productCode': 'p100',
  'capitalOrInstallment': 5000,
  'monthsTerm': 96,
  'mothsInitialTerm': 12,
  'openingCommission': 1.5,
  'minOpeningCommission': 60,
  'financeOpeningCommission': True,
  'interestRate': 5.5,
  'interestRateReferenceIndex': 0,
  'interestRateSecondaryReferenceIndex': 0,
  'interestRateSecondaryWithoutVinculation': 6.5,
  'interestRateSecondaryWithAllVinculation': 0,
  'interestRateSecondary': 6.5,
  'loanDate': '2019-06-13',
  'birthDate': '2001-06-13',
  'financeLoanProtectionInsurance': True,
  'percentageNotaryCosts': 0.003,
  'loanCalculationMethod': 0,
  'calculationBase': 4,
  'frecuencyAmortization': 12,
  'frecuencyInterestPay': 12,
  'calendarConvention': 0,
  'taeCalculationBaseType': 4,
  'lackMode': 0,
  'amortizationCarencyMonths': 0,
  'typeAmortization': 1,
  'insuranceCostSinglePremium': 0,
  'with123': False,
  'electricVehicle': False}}
#The scraping function
def scrape(amount, duration, params):

    params['wsInputs']['capitalOrInstallment'] = amount
    params['wsInputs']['monthsTerm'] = duration
    response = r.post('https://simuladores.bancosantander.es/WS/WSSantanderTotalLoan.asmx/Calculate', headers=headers, data=json.dumps(params))
    return json.loads(response.content)['d']


Amounts = [13000]
Durations = [ 48, 60, 72, 84, 96]
results = []
for amount in Amounts:
    for duration in Durations:
        result = scrape(amount, duration, params)
        result['Amount'] = amount
        result['Duration'] = duration
        results.append(result)

df = pd.DataFrame(results)

【问题讨论】:

  • Mod Note: 保持与问题相关的讨论。如果任何用户有问题,请在meta 上提出标记或发帖。请勿在此处相互讨论或威胁其他用户。
  • @keepAlive 不要在此用户的帖子下发表评论,也不要在此用户的帖子下发表评论。这是一个非官方的模组警告。如果继续下去,它将成为官方的。

标签: python ajax web-scraping request


【解决方案1】:

首先,正如@Richard 所说,您的代码没有问题。

您获得 7.03% 而不是 6.44% 的原因是因为您使用的贷款模拟器以某种方式作弊(以显得更有竞争力)。您的不同之处在于对Comisión de apertura financiada 的考虑。这意味着如果您将标准参数'openingCommission' 设置为0,您将获得6.45%。正好得到 6.44% 怎么样?接下来是一个建议。


解释(使用法语术语)

如果我计算与超参数 {14k€, 48months, 330.47€/m} 相关的 TEG 和 TAE,我得到 6.26% 和 6.44%。

但如果我做同样的计算,包括 210€ 的 Comisión de apertura financiada,我得到 ~7.03% 和 7.23%。

上面(和下面)的 i 代表internal rate of return (IRR),即环化方程(E1)的速率:


这意味着您应该考虑在您的工作流程中集成一个 IRR 求解器,使用可用的信息(计量、持续时间、总金额甚至费用)来重新计算 TAE。

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 2011-12-22
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多