为什么有些xpath绝对路径拿不到数据_无法从xpath找到数据

在尝试每分钟从finviz.com网站抓取并写入货币数据到csv文件时遇到问题。使用BeautifulSoup的findall方法无法获取所需数据。解决方案包括从图表标签中使用正则表达式和json库解析javascript对象,或者利用pandas或BeautifulSoup直接解析表格数据。
摘要由CSDN通过智能技术生成

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)

I would like to get the data like following format

name of the currency and values

example

GBP 0.27

解决方案

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]

or

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')

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值