【问题标题】:getting csv file from the url从 url 获取 csv 文件
【发布时间】:2017-11-23 09:51:03
【问题描述】:

我正在尝试下载 Web 门户上的 csv 文件,当手动下载时,我们登录到 url 并单击 Download CSV 按钮,然后它会提示保存。我们正在使用python3

我正在尝试通过 python 脚本来执行此操作,当我们执行此脚本时,我们会获得名称为 Download CSV 的 html 页面,当我们单击该页面时,我们会通过它获得一个 csv 文件。

import urllib.request
import requests


session = requests.session()
playload = {'j_username':'avinash.reddy', 'j_password':'password'}
r = session.post('https://url_of_the_portal/auth/login','data=playload')
r = session.get('URL_of_the_page_where_the_csv_file_exiests')
url='https://url_of_the_portal/review/download/bm_sis'
print ('done')
urllib.request.urlretrieve (url, "Download CSV")

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    我认为它应该看起来像这样 + 你的登录凭据。

    import csv
    import urllib2
    
    url = 'http://winterolympicsmedals.com/medals.csv'
    response = urllib2.urlopen(url)
    cr = csv.reader(response)
    
    for row in cr:
        print row
    

    否则...

    url = 'http://winterolympicsmedals.com/medals.csv'
    r = requests.get(url)
    text = r.iter_lines()
    reader = csv.reader(text, delimiter=',')
    

    否则...

    import requests
    from contextlib import closing
    import csv
    
    url = "http://download-and-process-csv-efficiently/python.csv"
    
    with closing(requests.get(url, stream=True)) as r:
        reader = csv.reader(r.iter_lines(), delimiter=',', quotechar='"')
        for row in reader:
            # Handle each row here...
            print row  
    

    How to read a CSV file from a URL with Python?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 2019-05-23
      • 2015-02-07
      • 2010-10-10
      相关资源
      最近更新 更多