Как преобразовать напечатанные ниже результаты в фрейм данных с помощью beautifulsoup на Yahoo Screener? - PullRequest
0 голосов
/ 05 августа 2020

Приведенный ниже код предоставит мне диапазон внутридневных цен для наиболее активных акций на Yahoo Screener

Как преобразовать их создание во фрейм данных?

Спасибо!

from bs4 import BeautifulSoup
import requests
url='https://in.finance.yahoo.com/most-active'
header = {'User-Agent': 's'}
response=requests.get(url,headers=header)
soup=BeautifulSoup(response.content, 'lxml')

for item in soup.select('.simpTblRow'):
    print(item.select('[aria-label=Symbol]')[0].get_text())

for item in soup.select('.simpTblRow'):
    print(item.select('[aria-label="Name"]')[0].get_text())

for item in soup.select('.simpTblRow'):
      print(item.select('[aria-label="Change"]')[0].get_text())
   
for item in soup.select('.simpTblRow'):
      print(item.select('[aria-label="% change"]')[0].get_text())

for item in soup.select('.simpTblRow'):
      print(item.select('[aria-label="Market cap"]')[0].get_text())
   
for item in soup.select('.simpTblRow'):
      print(item.select('[aria-label="Avg vol (3-month)"]')[0].get_text())

for item in soup.select('.simpTblRow'):
      print(item.select('[aria-label="Volume"]')[0].get_text())

1 Ответ

0 голосов
/ 05 августа 2020

Вы можете создать defaultdict со списком и добавить к списку каждого ключа в dict.

from bs4 import BeautifulSoup
import pandas as pd
import requests
from collections import defaultdict

url='https://in.finance.yahoo.com/most-active'
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}
response=requests.get(url,headers=header)
soup=BeautifulSoup(response.content, 'lxml')

data = defaultdict(list)

for item in soup.select('.simpTblRow'):
    data["Symbol"].append(item.select('[aria-label=Symbol]')[0].get_text())

for item in soup.select('.simpTblRow'):
    data["Name"].append(item.select('[aria-label="Name"]')[0].get_text())

for item in soup.select('.simpTblRow'):
    data["Change"].append(item.select('[aria-label="Change"]')[0].get_text())
   
for item in soup.select('.simpTblRow'):
    data["% change"].append(item.select('[aria-label="% change"]')[0].get_text())

for item in soup.select('.simpTblRow'):
    data["Market cap"].append(item.select('[aria-label="Market cap"]')[0].get_text())
   
for item in soup.select('.simpTblRow'):
    data["Avg vol (3-month)"].append(item.select('[aria-label="Avg vol (3-month)"]')[0].get_text())

for item in soup.select('.simpTblRow'):
    data["Volume"].append(item.select('[aria-label="Volume"]')[0].get_text())

df = pd.DataFrame(data)
print(df)

Вывод:

          Symbol                                   Name   Change % change Market cap Avg vol (3-month)    Volume
0         IDEA.NS                  Vodafone Idea Limited    -0.05   -0.57%   249.132B          502.413M  328.466M
1      YESBANK.NS                       Yes Bank Limited    +0.25   +2.08%    306.41B           71.135M  156.316M
2   TATAMOTORS.NS                    Tata Motors Limited    -1.60   -1.42%   364.219B           68.231M   95.878M
3         IDEA.BO                  Vodafone Idea Limited    -0.09   -1.03%   247.987B           97.612M   69.686M
4       532822.BO                  Idea Cellular Limited    -0.09   -1.03%    37.643B            1.056M   69.686M
5         ZEEL.NS  Zee Entertainment Enterprises Limited    +8.40   +6.22%   137.688B           25.077M   48.852M
6   TRIDENT-BL.NS                            TRIDENT LTD     0.00    0.00%        N/A               N/A       48M
7         SBIN.NS                    State Bank of India    -0.65   -0.34%      1.71T           71.068M   43.854M
8     RELIANCE.NS            Reliance Industries Limited  +141.60   +7.05%    14.542T            22.74M   42.474M
9    FCONSUMER.NS                Future Consumer Limited    +0.05   +0.43%    22.415B           10.807M   37.696M
10        BHEL.NS       Bharat Heavy Electricals Limited    +0.45   +1.24%   127.618B           81.129M   34.053M
11  IDFCFIRSTB.NS                IDFC First Bank Limited    +0.70   +2.68%   152.019B           46.588M   32.774M
12   ICICIBANK.NS                     ICICI Bank Limited    +7.75   +2.26%     2.272T            47.26M   30.789M
13    HDFCBANK.NS                      HDFC Bank Limited   +39.65   +3.96%     5.719T           18.522M   30.149M
14     VEDL-EQ.NS                        VEDANTA LIMITED    +3.75   +3.25%        N/A               N/A   25.879M
15        VEDL.NS                        Vedanta Limited    +3.75   +3.25%   440.937B           29.171M   25.879M
16     YESBANK.BO                       Yes Bank Limited    +0.24   +2.00%   306.171B            5.735M   26.218M
17      532648.BO                       Yes Bank Limited    +0.24   +2.00%    28.138B            1.105M   26.218M
18        BPCL.NS   Bharat Petroleum Corporation Limited    -9.25   -2.21%   886.141B           10.576M   25.606M
19         IOC.NS         Indian Oil Corporation Limited    +0.75   +0.87%   818.561B           22.285M   25.488M
20   TATAPOWER.NS         The Tata Power Company Limited    +0.95   +1.95%   134.427B           37.715M   25.277M
21    AXISBANK.NS                      Axis Bank Limited   +11.80   +2.83%     1.211T           40.473M   24.738M
22  BANKBARODA.NS                         Bank of Baroda    -0.35   -0.75%   214.857B            41.54M   24.648M
23        RCOM.NS        Reliance Communications Limited  -0.1000   -3.51%     8.977B            8.014M    22.86M
24    ASHOKLEY.NS                  Ashok Leyland Limited    +0.30   +0.61%   145.602B           42.522M   22.224M
...