【问题标题】:I am trying to scrape toll data using python from(https://www.expresslanes.com/map-your-trip) ,but i don't know how to scrape data using XHR我正在尝试使用 python 从 (https://www.expresslanes.com/map-your-trip) 抓取收费数据,但我不知道如何使用 XHR 抓取数据
【发布时间】:2021-11-21 14:48:36
【问题描述】:

网站链接:- Website 我想要每个入口点和每个出口点的所有组合 - 假设(Jones Branch Drive/Route 123)和(495 Express End(MD 附近))会给我这两条路线的组合。这就是我想要入口点和出口点的所有组合并将其存储到 excel 表中的方式。下面是收费数据中的参考图像,这些是以 json 格式存在的一些东西。 You can view data which is given below您可以查看下面给出的数据

【问题讨论】:

  • 您是否忘记将 Python 代码添加到您的问题中?另外,我相信 XHR 是特定于 JavaScript 的。话虽如此,您可能可以在 Python 中使用 scrapyseleniumrequests 中的任何一个
  • 代码在哪里?网址是什么?
  • Website link我没有提到任何代码,因为我不知道如何处理它。提供入口和出口点组合的网站部分似乎是静态的,但提供通行费价格的部分似乎是动态的,因为价格会不断变化。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: python json web-scraping xmlhttprequest


【解决方案1】:

该网站通过 Fetch/XRH 提供定价数据,因此您可以使用 python 库请求抓取这些数据。

要查看请求,请检查页面,然后导航到 Network -> Fetch/XRH,您会看到请求、请求标头和响应。

这是获取价格的示例代码:

import requests
import json
import pandas as pd

headers = {
    'authority': 'www.expresslanes.com',
    'sec-ch-ua': '"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"',
    'accept': 'application/json, text/javascript, */*; q=0.01',
    'x-requested-with': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://www.expresslanes.com/map-your-trip',
    'accept-language': 'en-US,en;q=0.9,ru;q=0.8',
}

response = requests.get('https://www.expresslanes.com/maps-api/infra-price-confirmed-all', headers=headers)

# Load the data
data = json.loads(response.content)

# Get the dataframe of required values from the resulted json
df = pd.json_normalize(data['response'])

df.head()

这会导致:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多