javascript - 在 Python 中使用回调下载 CSV

标签 javascript python html ajax python-requests

尝试从 Morningstar 中的按钮链接下载 CSV。该链接包含回调,因此它似乎阻止了下载。在 Chrome 中,使用“检查”功能,URL 为 http://financials.morningstar.com/finan/ajax/exportKR2CSV.html?&callback=?&t=XNAS:GOOGL®ion=usa&culture=en-US&cur=&order =asc,但它不会在粘贴到地址栏时自动下载 CSV。如何更新以下代码以通过回调从链接下载 CSV? http://financials.morningstar.com/ratios/r.html?t=GOOGL®ion=usa&culture=zh-CN

enter image description here

import requests

url = "http://financials.morningstar.com/finan/ajax/exportKR2CSV.html?&callback=?&t=XNAS:GOOGL&region=usa&culture=en-US&cur=&order=asc"
with requests.Session() as s:
    download = s.get(url)
    decoded_content = download.content.decode('utf-8')
    cr = csv.reader(decoded_content.splitlines(), delimiter=',')
    my_list = list(cr)
    for row in my_list:
        print(row)

my_list

输出:

<Response [204]>
[]

最佳答案

如果您确实选择了 selenium 路线,则很容易使用类选择器来定位下载按钮。您可以通过 chrome 选项指定下载选项,例如目录。

from selenium import webdriver

d = webdriver.Chrome()
d.get('http://financials.morningstar.com/ratios/r.html?t=GOOGL&region=usa&culture=en-US')
d.find_element_by_css_selector('.large_button').click()
d.quit()

关于javascript - 在 Python 中使用回调下载 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55409836/

相关文章:

javascript - overflow hidden 时检测用户滚动尝试

javascript - 在每个系列 Highcharts 中添加 Xaxis 数据

python - 在 Pandas 中使用多重索引过滤数据帧

javascript - 简单的 Javascript 文件无法正常工作

html - anchor 链接不触发

html - 谷歌字体不适用于 css

javascript - 如何从 jquery 或 javascript 中的字符串值中获取 "year"?

javascript - 从 JavaScript 中的对象复制某些属性的最有效方法是什么?

python - 如何用键初始化defaultdict?

python - 在 win32 中读取符号链接(symbolic link)/连接点的目标(通过 Python)