【问题标题】:How to Download .XML File From Website Through JavaScript Button Using Python如何使用 Python 通过 JavaScript 按钮从网站下载 .XML 文件
【发布时间】:2020-04-07 01:58:24
【问题描述】:

我正在尝试从该网站下载自定义 XML 文件: http://data.un.org/Data.aspx?d=CLINO&f=ElementCode:11;StatisticCode:01&c=1,2,5,17,18,44&s=CountryName:asc,WmoStationNumber:asc,StatisticCode:asc&v=1

我最熟悉的方法是使用pd.read_csv,但是这种情况下右键下载链接,复制链接地址生成:

javascript:Download('xml','CLINO','ElementCode:11;StatisticCode:01','s=CountryName:asc,WmoStationNumber:asc,StatisticCode:asc','c=1,2,5,17,18,44','');

我尝试了here 发布的解决方案,但不幸的是,该过程在第 4 步出现了偏差。

使用python,如何访问.xml文件下载并保存?

【问题讨论】:

    标签: javascript python url download python-requests


    【解决方案1】:
    import requests
    import pandas as pd
    
    params = {
        'Service': 'page',
        'DataFilter': 'ElementCode:11;StatisticCode:01',
        'DataMartId': 'CLINO',
        'UserQuery': '',
        'c': '1,2,5,17,18,44'
    }
    
    
    def main(url, params):
        with requests.Session() as req:
            allin = []
            for item in range(1, 23):
                print(f"Extracting Page# {item}")
                params['Page'] = item
                r = req.get(url, params=params)
                df = pd.read_html(r.content)[0]
                allin.append(df)
            new = pd.concat(allin)
            print(new)
            new.to_csv("Data.csv", index=False)
    
    
    main("http://data.un.org/Handlers/DataHandler.ashx", params)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多