Красивый суп извлекать данные из HTML исходного кода? - PullRequest
0 голосов
/ 09 февраля 2020

Я пытался извлечь "Reported EPS Basi c" с веб-страницы - "https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK". Данные 0,23, 0,2 появляются в следующем формате после запуска моих кодов. Как извлечь эти числа из следующего исходного кода?

"div class =" D (tb c) Ta (end) Pstart ( 6px) Pend (4px) Bxz (bb) Py (8px) BdB Bd c ($ seperatorColor) Miw (100px) Miw (156px) - pnclg "data-test =" fin-col "data-Reactiontid =" 292 "> 0,23

div class =" D (tb c) Ta (конец) Pstart (6px) Pend (4px) Bxz (bb) Py (8px) BdB Bd c ($ seperatorColor) Miw (100px) Miw (156px) - pnclg Bg c ($ lv1BgColor) fi-строка: h_Bg c ($ hoverBgColor) "data-test =" fin-col "data-Reactiontid =" 293 "> 0,20

My коды:

url="https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK"
result = requests.get(url)
result.raise_for_status()
result.encoding = "utf-8"


src = result.content
soup = BeautifulSoup(src, 'lxml')
#soup = BeautifulSoup(src, 'html5lib')
#print(soup.prettify())
print(soup)

with open('soup.txt','w') as f:
    f.write(str(src))

1 Ответ

3 голосов
/ 09 февраля 2020

Попробуйте,

import requests
import bs4

url = 'https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK'
data = requests.get(url)

soup = bs4.BeautifulSoup(data.text,'html.parser')

soup.find_all('div',attrs={"data-reactid":"292"})[0].text
soup.find_all('div',attrs={"data-reactid":"293"})[0].text
...