首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从investing.com中抓取数据?

如何从investing.com中抓取数据?
EN

Stack Overflow用户
提问于 2020-10-22 15:07:42
回答 1查看 3K关注 0票数 1

我想从https://au.investing.com/currencies/eur-usd上摘录欧元/美元的5分钟技术总结,但我不知道该怎么做。我试过使用requests模块,但它显示我被禁止访问该网站。

EN

回答 1

Stack Overflow用户

发布于 2020-10-22 15:37:58

服务器从外部URL加载数据。您可以使用requests模块来加载它。例如,打印candles/times:

代码语言:javascript
复制
import json
import datetime
import requests 
from bs4 import BeautifulSoup


url = 'https://au.investing.com/common/modules/js_instrument_chart/api/data.php?pair_id=1&pair_id_for_news=1&chart_type=area&pair_interval=300&candle_count=120&events=yes&volume_series=yes&period='
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0',
    'X-Requested-With': 'XMLHttpRequest',
    'Referer': 'https://au.investing.com/currencies/eur-usd'}
data = requests.get(url, headers=headers).json()

# uncomment this to print all data:
# print( json.dumps(data, indent=4) )

for candle in data['candles']:
    t = datetime.datetime.fromtimestamp(candle[0] // 1000)
    print('{!s:<20} {:<10} {:<10} {:<10}'.format(t, *candle[1:]))

打印:

代码语言:javascript
复制
2020-10-21 21:40:00  1.1859     123        1004      
2020-10-21 21:45:00  1.1859     184        1127      
2020-10-21 21:50:00  1.1862     173        1311      
2020-10-21 21:55:00  1.186      174        1484      
2020-10-21 22:00:00  1.1863     291        1658      
2020-10-21 22:05:00  1.1864     226        1949      
2020-10-21 22:10:00  1.1863     197        2175      
2020-10-21 22:15:00  1.1863     112        2372      
2020-10-21 22:20:00  1.1863     149        2484      
2020-10-21 22:25:00  1.1862     93         2633      
2020-10-21 22:30:00  1.1863     111        2726      
2020-10-21 22:35:00  1.1861     121        2837      
2020-10-21 22:40:00  1.1862     151        2958      
2020-10-21 22:45:00  1.1861     34         3109      
2020-10-21 22:50:00  1.1861     236        3143      
2020-10-21 22:55:00  1.186      148        3379      
2020-10-21 23:00:00  1.1861     202        3527      
2020-10-21 23:05:00  1.1859     264        3729      
2020-10-21 23:10:00  1.186      162        3993      
2020-10-21 23:15:00  1.1859     223        4155      

...and so on.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64476984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档