Извлечение таблицы с веб-сайта с помощью Python BeautifulSoup

#python-3.x #pandas #web-scraping #beautifulsoup

#python-3.x #pandas #очистка веб-страниц #beautifulsoup

Вопрос:

Я хочу извлечь таблицы с веб-сайта по ссылке (« https://www.ups.com/us/en/shipping/surcharges/fuel-surcharges.page ) кто-нибудь может сказать мне, как это сделать?

Комментарии:

1. Каков ваш код на данный момент?

Ответ №1:

Данные загружаются с помощью JavaScript с внешнего URL. Вы можете использовать этот пример, как его загрузить:

 import json
import requests 


json_url = 'https://www.ups.com/assets/resources/fuel-surcharge/us.json'
data = requests.get(json_url).json()

#uncomment this to see all data:
# print(json.dumps(data, indent=4))

# print some data to screen:
for k, v in data['FuelSurchargeResponse'].items():
    if k.endswith('_en'):
        for kk, vv in v.items():
            print(k, kk)
            print('-' * 80)
            for vvv in vv:
                print(*vvv.values())
            print()
  

С принтами:

 FuelSurchargeRates_en USGCJetFuelDomesticInternationalExport
--------------------------------------------------------------------------------
USD 0.61 USD 0.66 1.50%
USD 0.66 USD 0.71 1.75%
USD 0.71 USD 0.76 2.00%
USD 0.76 USD 0.81 2.25%
USD 0.81 USD 0.86 2.50%
USD 0.86 USD 0.91 2.75%
USD 0.91 USD 0.96 3.00%
USD 0.96 USD 1.01 3.25%
USD 1.01 USD 1.06 3.50%
USD 1.06 USD 1.11 3.75%
USD 1.11 USD 1.16 4.00%
USD 1.16 USD 1.21 4.25%
USD 1.21 USD 1.26 4.50%
USD 1.26 USD 1.31 4.75%
USD 1.31 USD 1.36 5.00%

FuelSurchargeRates_en USDiesel
--------------------------------------------------------------------------------
USD 1.46 USD 1.58 4.50%
USD 1.58 USD 1.70 4.75%
USD 1.70 USD 1.82 5.00%
USD 1.82 USD 1.94 5.25%
USD 1.94 USD 2.06 5.50%
USD 2.06 USD 2.18 5.75%
USD 2.18 USD 2.30 6.00%
USD 2.30 USD 2.42 6.25%
USD 2.42 USD 2.54 6.50%
USD 2.54 USD 2.66 6.75%
USD 2.66 USD 2.78 7.00%
USD 2.78 USD 2.90 7.25%
USD 2.90 USD 3.02 7.50%
USD 3.02 USD 3.14 7.75%
USD 3.14 USD 3.26 8.00%

FuelSurchargeRates_en USGCJetFuelInternationalImport
--------------------------------------------------------------------------------
USD 0.77 USD 0.80 0.00% 3.75%
USD 0.80 USD 0.83 0.25% 4.00%
USD 0.83 USD 0.86 0.50% 4.25%
USD 0.86 USD 0.89 0.75% 4.50%
USD 0.89 USD 0.92 1.00% 4.75%
USD 0.92 USD 0.95 1.25% 5.00%
USD 0.95 USD 0.98 1.50% 5.25%
USD 0.98 USD 1.01 1.75% 5.50%
USD 1.01 USD 1.04 2.00% 5.75%
USD 1.04 USD 1.07 2.25% 6.00%
USD 1.07 USD 1.10 2.50% 6.25%
USD 1.10 USD 1.13 2.75% 6.50%
USD 1.13 USD 1.16 3.00% 6.75%
USD 1.16 USD 1.19 3.25% 7.00%
USD 1.19 USD 1.22 3.50% 7.25%

SurchargeHistory_en RevenueSurchargeHistory
--------------------------------------------------------------------------------
10/19/2020 6.25% 3.25% 1.75% 5.50%
10/12/2020 6.25% 3.25% 1.75% 5.50%
10/05/2020 6.25% 3.25% 1.50% 5.25%
09/28/2020 6.50% 3.50% 2.00% 5.75%
09/21/2020 6.50% 3.25% 1.75% 5.50%
09/14/2020 6.50% 4.00% 3.00% 6.75%
09/07/2020 6.50% 4.00% 3.00% 6.75%
08/31/2020 6.50% 3.75% 2.75% 6.50%
08/24/2020 6.50% 4.00% 2.75% 6.50%
08/17/2020 6.50% 4.00% 2.75% 6.50%
08/10/2020 6.50% 3.75% 2.75% 6.50%
08/03/2020 6.50% 4.00% 2.75% 6.50%
07/27/2020 6.50% 3.75% 2.50% 6.25%