【问题标题】:Extracting table from UPS website using BeautifulSoup使用 BeautifulSoup 从 UPS 网站提取表格
【发布时间】:2020-10-15 12:02:18
【问题描述】:

我试图使用下面的代码通过表格 ID 提取我们的柴油附加费,但直到 <thead> 而不是 <tbody> 之前它的唯一读数是我做错了什么?

url = 'https://www.ups.com/us/en/shipping/surcharges/fuel-surcharges.page'
response = requests.get(url)
print(response.status_code)
soup = BeautifulSoup(response.content,"html.parser")
tables = soup.find(id="USDiesel")
print(tables)
tables_all = []
for tr in tables.find_all('tr'):
    data = []
    for td in tr.find_all('td'):
        data.append(td.text.strip())
    tables_all.append(data)
table_df = pd.DataFrame(tables_all)
headers = table_df.iloc[0]
UPS_Gfuelsurcharge_df  = pd.DataFrame(table_df.values[1:], columns=headers)
print(UPS_Gfuelsurcharge_df)

【问题讨论】:

    标签: python html pandas web-scraping beautifulsoup


    【解决方案1】:

    网站使用ajax加载表格数据。

    import requests
    
    res = requests.get("https://www.ups.com/assets/resources/fuel-surcharge/us.json")
    
    print(res.json())
    

    【讨论】:

    • 您能否建议我如何提取相对较新的表格
    • @adolfsingh 您以 json 的形式获取值。循环遍历 json 值。参考 - realpython.com/python-json
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2020-10-25
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    相关资源
    最近更新 更多