Home > Mobile >  How to find elements that match specific conditions selenium
How to find elements that match specific conditions selenium

Time:11-22

i want to crawl data in web, but i don't know how to get data from these tags i don't know how to get data from these tags. Please help me

from selenium import webdriver
import pandas as pd
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
browser = webdriver.Chrome(executable_path="./chromedriver.exe")
idx = 0
data = []
title = []


   #print("Process 300 days from {}-{}-{}".format(current_date.day, current_date.month, current_date.year))
url = 'https://24hmoney.vn/stock/HAG/financial-report'
web = browser.get(url)
#Click nut theo quy
btn1 = browser.find_element(By.XPATH, "/html/body/div[1]/div/div/div[2]/div[1]/div[4]/div[2]/div[1]")
btn1.click()
#click hien thi tang giam so voi cung ki
#btn2 = browser.find_element(By.XPATH,"/html/body/div[1]/div/div/div[2]/div[1]/div[4]/div[3]/div[1]/span")

#btn2.click()
lai = browser.find_elements(By.CSS_SELECTOR,'p')
for raw in lai:
    data.append(raw.text)
    #print(raw.text)
tieude = browser.find_elements(By.CLASS_NAME,'sticky-col.first-col')

for raw2 in tieude:
    title.append(raw2.text)
    print(raw2.text)
    
#df = pd.DataFrame(data,columns=["HAG"])
df = pd.DataFrame(title,columns=["Tieude"])
df.to_csv("HAG.csv",index=False)
#a = input()

CodePudding user response:

Maybe the following code will solve your issue?

import requests
import pandas as pd
from bs4 import BeautifulSoup as bs

pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}

url = 'https://24hmoney.vn/stock/HAG/financial-report'

r = requests.get(url, headers=headers)
soup = bs(r.text, 'html.parser')
table = soup.select_one('div[] table')
df = pd.read_html(str(table))[0]
print(df)

Result in terminal:

Tiêu đề Q3/22   % Q3/21 Q2/22   % Q2/21 Q1/22   % Q1/21 Q4/21   % Q4/20 Q3/21   % Q3/20 Q2/21   % Q2/20 Q1/21   % Q1/20 Q4/20   % Q4/19
0   Doanh thu   1441.4  160.1%  1233.6  125.2%  802.6   182.3%  743.7   -19.2%  554.1   -20.9%  547.7   -15.4%  284.4   -66%    920.4   51%
1   Các khoản giảm trừ  NaN NaN 6.2 -81.3%  NaN NaN NaN NaN NaN NaN 3.4 68.3%   18.5    -678.5% 6.7 5.7%
2   Doanh thu thuần 1441.4  160.1%  1227.4  125.5%  802.6   201.9%  743.7   -18.6%  554.1   -20.9%  544.3   -14.6%  265.8   -68.1%  913.7   51.6%
3   Giá vốn hàng bán    1160.6  -207.3% 1051.7  -115.3% 512.8   -140.3% 511.5   52.7%   377.6   50.1%   488.4   3%  213.4   61.3%   1082.2  -74.1%
4   Lợi nhuận gộp   280.8   59.1%   175.7   214.4%  289.8   452.8%  232.3   237.9%  176.5   414.3%  55.9    -58.1%  52.4    -81.5%  -168.5  -783.9%
5   Thu nhập tài chính  117.5   -10.9%  95.4    -24.8%  192.4   -44.9%  127.6   -83.7%  131.9   -5.3%   126.8   -34.3%  349.4   122.3%  783.6   203.9%
6   Chi phí tài chính   166.0   76% 875.9   -410.8% 185.9   13.4%   254.7   150.6%  691.5   -161.9% 171.5   -39.8%  214.8   33.6%   503.4   -44.1%
7   Chi phí tiền lãi    166.9   -0.2%   223.6   -35.6%  162.7   18.6%   167.4   66.3%   166.5   21.9%   165.0   25.9%   199.8   25.3%   496.9   -72.7%
8   Lãi/lỗ từ công ty liên doanh    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN -7.6    -1,149% 1.8 -18.8%  4.9 -86.8%
9   Chi phí bán hàng    58.6    -51.5%  90.5    -191.7% 52.1    -202.7% 42.3    34.5%   38.7    47.6%   31.0    76.5%   17.2    79.6%   64.6    14.5%
10  Chi phí quản lý doanh nghiệp    181.1   -60.4%  950.6   322.9%  5.2 101.4%  404.5   56% 457.0   395.4%  224.8   424.4%  367.3   -272.5% 919.8   -890.1%
11  Lãi/lỗ từ hoạt động kinh doanh  354.8   909%    255.2   29.3%   249.3   227.4%  167.7   119.3%  35.2    108.6%  197.3   10,174% -195.7  -202.8% -867.8  -258.5%
12  Thu nhập khác   2.8 35% 24.8    455.4%  5.7 -81.7%  44.0    66.5%   2.1 7.2%    4.5 -85.2%  31.1    68.9%   26.5    140.4%
13  Chi phí khác    -7.4    56.3%   -56.0   66.7%   -15.3   81.7%   -143.8  78.8%   -17.0   89.7%   -168.3  -98.2%  -83.5   -153.3% -679.1  -141.2%
14  Thu nhập khác, ròng -4.6    69.2%   -31.2   81% -9.6    81.7%   -99.8   84.7%   -14.9   90.8%   -163.8  -198.8% -52.4   -260.2% -652.7  -141.2%
15  Lãi/lỗ từ công ty liên doanh    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
16  LỢI NHUẬN TRƯỚC THUẾ    350.2   1,629%  224.0   569.6%  239.8   196.6%  67.9    104.5%  20.2    103.5%  33.5    163.2%  -248.1  -213.3% -1520.5 -196.6%
17  Thuế thu nhập doanh nghiệp – hiện thời  1.2 NaN 1.4 -333.9% 0.2 NaN 0.0 96.6%   NaN NaN 0.3 -76%    NaN NaN 1.1 -27.7%
18  Thuế thu nhập doanh nghiệp – hoãn lại   20.5    1,267%  42.2    -3.9%   18.4    -89.7%  28.6    801.7%  1.5 -19.5%  43.9    1,847%  179.4   15,941% 4.1 -102.4%
19  Chi phí thuế thu nhập doanh nghiệp  19.4    1,191%  40.8    -6.3%   18.2    -89.8%  28.6    656.6%  1.5 -13.8%  43.6    1,718%  179.4   18,238% 5.1 -103%
20  LỢI NHUẬN SAU THUẾ TNDN 369.5   1,599%  264.9   243.7%  258.0   475.2%  96.5    106.3%  21.7    103.8%  77.1    238.6%  -68.8   12.1%   -1525.6 -344.5%
21  Lợi ích của cổ đông thiểu số    8.8 548.1%  -14.9   -3,505% 8.0 177%    -45.7   87% -2.0    99.5%   0.4 100.2%  -10.3   -14.7%  -352.1  11.8%
22  Lợi nhuận của Cổ đông của Công ty mẹ    360.7   1,421%  279.7   265%    250.0   528%    142.2   112.1%  23.7    112.7%  76.6    -56.6%  -58.4   15.6%   -1173.5 -2,193%
23  EPS 4QGN (đ)    389.0   1,396%  301.0   262.6%  270.0   528.6%  153.0   112.1%  26.0    112.9%  83.0    -56.5%  -63.0   16% -1265.0 -540.8%
  • Related