Текст с веб-страницы, используя BeautifulSoup - PullRequest
0 голосов
/ 20 ноября 2018

Я пытаюсь извлечь некоторые данные из https://markets.cboe.com/europe/equities/market_share/index/all/ с помощью Python

В частности, для показателя "Рынок всего не отображаемого объема", я пробовал несколько способов, используя BeautifulSoup, но ни один из них не кажетсядоставить меня туда.

есть идеи?

Ответы [ 2 ]

0 голосов
/ 20 ноября 2018

Проблема в том, что id постоянно меняется динамически.В противном случае, я бы просто использовал это, но не могу.Предполагая, что выходное значение - это то, что вы ищете, это должно работать, также до тех пор, пока содержимое не изменяется или не перемещается.

from bs4 import BeautifulSoup as bs
import requests

url = 'https://markets.cboe.com/europe/equities/market_share/index/all/'
page = requests.get(url)
html = bs(page.text, 'lxml')
total_volume = html.findAll('td', class_='idx_val')
print(total_volume[645].text)

Output:
€4,378,517,621
0 голосов
/ 20 ноября 2018

Я бы посоветовал дать html-читателю pandas шанс:

import pandas as pd

# Read in all tables at this address as pandas dataframes
results = pd.read_html('https://markets.cboe.com/europe/equities/market_share/index/all')

# Grab the second table founds
df = results[1]
# Set the first column as the index
df = df.set_index(0)
# Switch columns and indexes
df = df.T
# Drop any columns that have no data in them
df = df.dropna(how='all', axis=1)
# Set the column under "Displayed Price Venues" as the index
df = df.set_index('Displayed Price Venues')
# Switch columns and indexes again
df = df.T

# Aesthetic. Don't like having an index name myself! 
del df.index.name

# Separate the three subtables from each other!  
displayed = df.iloc[0:18]
non_displayed = df.iloc[18:-1]
total = df.iloc[-1]

Вы также можете сделать это более агрессивным способом (тот же код, но без разбивки шагов):

import pandas as pd

# Read in all tables at this address as pandas dataframes
results = pd.read_html('https://markets.cboe.com/europe/equities/market_share/index/all')

# Do all the stuff above in one go
df = results[1].set_index(0).T.dropna(how='all',axis=1).set_index('Displayed Price Venues').T

# Aesthetic. Don't like having an index name myself! 
del df.index.name

# Separate the three subtables from each other!  
displayed = df.iloc[0:18]
non_displayed = df.iloc[18:-1]
total = df.iloc[-1]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...