我正在尝试使用此代码将结果显示在页面底部,但由于某种原因,它无法正常工作,并且文件中未保存任何内容,只是空白。任何人都知道代码中有什么问题。谢谢
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import requests
from bs4 import BeautifulSoup
url = 'http://www.tradingview.com/screener'
driver = webdriver.Firefox()
driver.get(url)
# will give a list of all tickers
tickers = driver.find_elements_by_css_selector('a.tv-screener__symbol')
# will give a list of all EMPs
emps = driver.find_elements_by_xpath('//tbody/tr/td[10]')
# will give a list of all sectors
sectors = driver.find_elements_by_xpath('//tbody/tr/td[11]')
soup = BeautifulSoup(driver.page_source, 'lxml')
main_div = soup.find('div', attrs = {'id':'js-screener-container'})
rows = main_div.find_all('tr', class_="tv-data-table__row tv-data-table__stroke tv-screener-table__result-row")
data = []
for rows_set in (rows):
for row in rows_set:
row_data = []
for cell in row.find_all('td'):
val = cell.a.get_text()
row_data.append(val)
data.append(row_data)
import pandas
pandas.DataFrame(data).to_csv("AAA.csv", header=False)
答案 0 :(得分:1)
您可以尝试以下方法:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path = r'C:/FireFox/geckodriver.exe')
driver.get("http://www.tradingview.com/screener")
wait = WebDriverWait(driver, 20)
# will give a list of all tickers
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "a.tv-screener__symbol")))
tickers = driver.find_elements_by_css_selector("a.tv-screener__symbol")
print(len(tickers))
for ticker in tickers:
print(ticker.text)
# will give a list of all EMPs
wait.until(EC.presence_of_all_elements_located((By.XPATH, "//tbody/tr/td[10]")))
emps = driver.find_elements_by_xpath('//tbody/tr/td[10]')
print(len(emps))
for emp in emps:
print(emp.text)
# will give a list of all sectors
wait.until(EC.presence_of_all_elements_located((By.XPATH, "//tbody/tr/td[11]")))
sectors = driver.find_elements_by_xpath('//tbody/tr/td[11]')
print(len(sectors))
for sector in sectors:
print(sector.text)
此后,您可以像执行操作一样将数据保存到文件中。
输出:
150
AAPL
AMZN
GOOG
GOOGL
MSFT
FB
BABA
BRK.A
BRK.B
JPM
XOM
JNJ
V
RDS.B
RDS.A
BAC
WFC
WMT
UNH
T
CVX
INTC
HD
PFE
VZ
TM
MA
PG
CSCO
PTR
NVS
BA
KO
HSBC
TSM
ORCL
CHL
BUD
NFLX
C
TOT
MRK
UN
UL
DIS
BP
DWDP
PEP
CMCSA
ABBV
NVDA
SAP
IBM
PM
BHP
BBL
MCD
NKE
AMGN
ADBE
BTI
MMM
GE
MDT
SNP
ACN
RY
UNP
MO
HON
TXN
ABT
TD
SNY
AVGO
CRM
GSK
UTX
PYPL
BKNG
RIO
LLY
GILD
SLB
UPS
BMY
EQNR
NVO
COST
DEO
LFC
FOXA
FOX
SAN
AZN
LMT
AXP
BIDU
QCOM
TMO
GS
ASML
MS
USB
CAT
COP
HDB
NEE
BLK
LOW
MUFG
KHC
WBK
ABEV
CEO
EOG
BNS
CHTR
DHR
E
SU
SCHW
SBUX
CVS
VALE
VOD
BDX
OXY
AMT
VMW
ITUB
SYK
PNC
PBR
PBR.A
BIIB
SNE
ANTM
FDX
WBA
MDLZ
AET
MU
PUK
CB
ENB
EPD
TJX
AABA
CNI
150
123K
566K
85.05K
85.05K
124K
27.742K
50.097K
377K
377K
253.707K
69.6K
134K
15K
86K
86K
208K
265.7K
2.3M
260K
249.24K
51.9K
102.7K
413K
90.2K
154.7K
369.124K
13.4K
95K
72.9K
494.297K
124K
140.8K
61.8K
228.899K
48.602K
137K
464.656K
200K
5.4K
209K
98.277K
69K
161K
160.566K
199K
74K
98K
263K
164K
29K
11.528K
88.543K
366.6K
80.6K
26.146K
26.146K
235K
74.4K
20.8K
17.973K
55.761K
91.536K
313K
86K
446.225K
425K
79.308K
41.735K
8.3K
131K
29.714K
99K
83.16K
106.556K
14K
29K
98.462K
205K
18.7K
22.9K
46.807K
40.655K
10K
100K
454K
23.7K
20.245K
42.688K
133K
30.051K
102.297K
21.7K
21.7K
201.9K
61.1K
100K
55K
39.343K
33.8K
70K
37.3K
16.994K
57.81K
72.402K
98.4K
11.2K
88.253K
13.9K
14K
200K
117.321K
39K
32.033K
51.4K
14.783K
2.664K
89.809K
94.8K
67K
32.934K
12.381K
18.2K
277K
246K
65.539K
106.135K
41.9K
11K
4.752K
21.7K
99.618K
33K
50.358K
62.703K
62.703K
7.3K
117.3K
56K
117K
235K
83K
47.95K
34.1K
27.151K
31K
12.7K
7K
249K
8.6K
24.812K
150
Technology
Consumer Cyclicals
Technology
Technology
Technology
Technology
Technology
Financials
Financials
Financials
Energy
Healthcare
Technology
Energy
Energy
Financials
Financials
Consumer Non-Cyclicals
Healthcare
Telecommunications Services
Energy
Technology
Consumer Cyclicals
Healthcare
Telecommunications Services
Consumer Cyclicals
Technology
Consumer Non-Cyclicals
Technology
Energy
Healthcare
Industrials
Consumer Non-Cyclicals
Financials
Technology
Technology
Telecommunications Services
Consumer Non-Cyclicals
Technology
Financials
Energy
Healthcare
Consumer Non-Cyclicals
Consumer Non-Cyclicals
Consumer Cyclicals
Energy
Basic Materials
Consumer Non-Cyclicals
Consumer Cyclicals
Healthcare
Technology
Technology
Technology
Consumer Non-Cyclicals
Basic Materials
Basic Materials
Consumer Cyclicals
Consumer Cyclicals
Healthcare
Technology
Consumer Non-Cyclicals
Industrials
Industrials
Healthcare
Energy
Technology
Financials
Industrials
Consumer Non-Cyclicals
Industrials
Technology
Healthcare
Financials
Healthcare
Technology
Technology
Healthcare
Industrials
Technology
Consumer Cyclicals
Basic Materials
Healthcare
Healthcare
Energy
Industrials
Healthcare
Energy
Healthcare
Consumer Cyclicals
Consumer Non-Cyclicals
Financials
Consumer Cyclicals
Consumer Cyclicals
Financials
Healthcare
Industrials
Financials
Technology
Technology
Healthcare
Financials
Technology
Financials
Financials
Industrials
Energy
Financials
Utilities
Financials
Consumer Cyclicals
Financials
Consumer Non-Cyclicals
Financials
Consumer Non-Cyclicals
Energy
Energy
Financials
Consumer Cyclicals
Healthcare
Energy
Energy
Financials
Consumer Cyclicals
Consumer Non-Cyclicals
Basic Materials
Telecommunications Services
Healthcare
Energy
Financials
Technology
Financials
Healthcare
Financials
Energy
Energy
Healthcare
Technology
Healthcare
Industrials
Consumer Non-Cyclicals
Consumer Non-Cyclicals
Healthcare
Technology
Financials
Financials
Energy
Energy
Consumer Cyclicals
Financials
Industrials