问题描述
我试图每分钟提取一次数据并将其写入csv文件,但我不这样做.因为我是这个广阔的数据科学世界的新手.
I tried to extract the data every minute and write the data into csv file but I coun't do it. Since I am new to this broad data science world.
我尝试使用汤库查找所有内容,但未显示数据.
I tried findall with soup library but not showing the data.
import requests
from bs4 import BeautifulSoup
page = requests.get('https://finviz.com/forex_performance.ashx')
soup = BeautifulSoup(page.content, 'html.parser')
forex = soup.find_all("div", {"class": "content "})
print(forex)
我想获取如下格式的数据货币名称和值例子 0.27英镑
I would like to get the data like following formatname of the currency and valuesexample GBP 0.27
推荐答案
对于第一部分,您可以使用统计图选项卡并正则表达式输出包含统计图数据的必需javascript对象.您可以使用json
库对此进行解析.
For the first part you can use the charts tab and regex out the required javascript object which houses the chart data. You can parse that with json
library.
import re
import json
import requests
r = requests.get('https://finviz.com/forex_charts.ashx')
p = re.compile(r'var tiles = (.*);')
data = json.loads(p2.findall(r.text)[0])
one_day_relative_performance_usd = [(data[item]['label'], data[item]['change']) for item in data]
第二次您可以使用熊猫或请求
For the second you can use pandas or requests
import pandas as pd
table = pd.read_html('finviz.com/forex_performance.ashx')[2]
或
from bs4 import BeautifulSoup as bs
import requests
r = requests.get('https://finviz.com/forex_performance.ashx')
soup = bs(r.content, 'lxml')
table = soup.select_one('#forex_performance table')
网络流量中有一个API调用
There is an API call in the web traffic
https://finviz.com/api/forex_perf.ashx
但目前至少在我的浏览器中不起作用.
but that is currently not working at least in my browser.
这篇关于无法从xpath找到数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!