Download a .csv file using requests.get() in Python

huangapple go评论131阅读模式
英文:

Download a .csv file using requests.get() in Python

问题

我想使用requests.get()从这个页面https://data.anbima.com.br/certificado-de-recebiveis?view=precos下载一个.CSV文件。当我使用检查功能时,没有直接链接到文件。

这个页面可能使用API调用来下载。我已经在Chrome的网络面板上研究了请求,但我不知道如何在Python中传递正确的参数。

我正在尝试使用requests.get(link, params=)。

英文:

I want to download a .CSV file from this page https://data.anbima.com.br/certificado-de-recebiveis?view=precos, using requests. Get(). When I use the Inspect, there is no link directly to the file.

The page probably uses an API call to Download. I've studied the request using the Network panel on Chrome, but I got stuck in how to pass the right parameters in Python.

I'm trying to use the requests.get( link, params= )

答案1

得分: 1

尝试使用Pandas库,它现在可以自动从URL下载CSV文件 Pandas Read CSV Documentation

import pandas as pd
csv_url = 'csv_url'
df = pd.read_csv(csv_url)

或者,您可以使用lxml和requests库来抓取CSV链接。从您的网页中获取链接元素的xpath,并将链接URL字符串保存为变量。

注意:lxml返回的是一个列表。

from lxml import html
from lxml import etree
import requests

url = 'website'
# 打开网页
session_requests = requests.session()
result = session_requests.get(url)
tree = html.fromstring(result.text)

# xpath到CSV URL(右键点击查看元素以获取xpath)
url_list = tree.xpath('xpath')  # 要获取链接,请在xpath的末尾添加 /@href

# xpath返回链接为列表,这只是从列表中提取链接文本
csv_url = url_list[0]
英文:

Try using the Pandas library, it automatically downloads csv's from urls now Pandas Read CSV Documentation.

import pandas as pd
csv_url = 'csv_url'
df=pd.read_csv(csv_url)

Alternatively you can us lxml and requests libraries to scrape for the csv link. Grab the xpath of the link element from your webpage and save the link url string as a var.

Note lxml returns as a list.

from lxml import html
from lxml import etree
import requests

            
url = 'website'
#Open webpage
session_requests = requests.session()
result = session_requests.get(url)
tree = html.fromstring(result.text)

#xpath to csv URL (inspect element and right click to get xpath)

url_list = tree.xpath('xpath') #to get the link add /@href to the end of your xpath

#xpath returns link as a list, this just pulls link text out of the list
csv_url = url_list[0]

huangapple
  • 本文由 发表于 2023年8月11日 02:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定