from selenium import webdriver
import time
url = 'https://xueqiu.com/?category=livenews'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(5)
driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[1]/a[2]').click()
## 下拉到页面底端
for y in range(200):
js = 'window.scrollBy(0,100)' ## 下拉滚动条
driver.execute_script(js) ##执行js代码
time.sleep(0.01)
continue
## 下拉到页面底端出现加载更多按钮
for t in range(20):
## 点击加载更多按钮 重复20次
driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[2]/a').click()
for j in range(200): ## 下拉
js = 'window.scrollBy(0,100)'
driver.execute_script(js)
time.sleep(0.01)
#js = 'window.scrollTo(0,10000)'
#driver.execute_script(js)
#driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[2]/a').click()
el_list = driver.find_elements_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[2]/div[1]/div/table/tbody/tr/td[3]/a')
for el in el_list:
print(el.text,el.get_attribute('href'))
##################################################################################
## 无界面模式
from selenium import webdriver
import time
url = 'https://xueqiu.com/?category=livenews'
## 环境配置
opt = webdriver.ChromeOptions()
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=opt)
driver.get(url)
driver.implicitly_wait(5)
driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[1]/a[2]').click()
for y in range(200):
js = 'window.scrollBy(0,100)'
driver.execute_script(js)
time.sleep(0.01)
continue
for t in range(20):
driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[2]/a').click()
for j in range(200):
js = 'window.scrollBy(0,100)'
driver.execute_script(js)
time.sleep(0.01)
#js = 'window.scrollTo(0,10000)'
#driver.execute_script(js)
#driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[2]/a').click()
el_list = driver.find_elements_by_xpath('//*[@id="app"]/div[3]/div[1]/div[2]/div[2]/div[1]/div/table/tbody/tr/td[3]/a')
for el in el_list:
print(el.text,el.get_attribute('href'))



