1、安裝 selinium 和 chrome 瀏覽器
# pip install selenium
# wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# dpkg -i google-chrome-stable_current_amd64.deb
## 可以看到 chrome 瀏覽器的版本是 126.0.6478.126-1
# dpkg -l | grep chrome
ii google-chrome-stable 126.0.6478.126-1 amd64 The web browser from Google
2、安裝 chromedriver
chromedriver 的版本要和 chrome 瀏覽器對應,比如都要是 126.xxx.xxx.xxx
根據(jù)上面的安裝的 chrome 瀏覽器版本 126.0.6478.126-1 ,從 https://googlechromelabs.github.io/chrome-for-testing/#stable 下載對應的 chromedriver 版本,比如 https://storage.googleapis.com/chrome-for-testing-public/126.0.6478.126/linux64/chromedriver-linux64.zip
# cd /opt/
# wget https://storage.googleapis.com/chrome-for-testing-public/126.0.6478.126/linux64/chromedriver-linux64.zip
# unzip chromedriver-linux64.zip
# ls /opt/chromedriver-linux64/chromedriver
3、實現(xiàn)爬取的代碼demo
# cat get_bn_listing_demo.py
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pandas as pd
from selenium.webdriver.chrome.options import Options
import datetime as dtdt
def main():
# 設置Chrome瀏覽器無頭模式
options = Options()
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# 第 2 步下載的 chromedriver 路徑
chromedriver_path = "/opt/chromedriver-linux64/chromedriver"
driver = webdriver.Chrome(service=Service(executable_path=chromedriver_path), options=options)
# 要爬取的示例網(wǎng)頁地址,獲取最新公告的 時間 和 標題
url = f"https://www.binance.com/en/support/announcement/new-cryptocurrency-listing?c=48&navId=48&hl=en"
titles = []
dts = []
driver.get(url)
time.sleep(5)
title = driver.find_elements(By.CLASS_NAME, 'css-1yxx6id')
for t in title:
titles.append(t.text)
# print(titles)
dt = driver.find_elements(By.CLASS_NAME, 'css-eoufru')
for t in dt:
dts.append(t.text)
# print(dts)
driver.quit()
row = {
"title": titles,
"datetime": dts,
}
df = pd.DataFrame(row)
# print(df)
filtered_df = df[df['title'].str.contains('Will List')]
print(filtered_df)
for index, row in filtered_df.iterrows():
print(f"Title: {row['title']}")
print(f"Date: {row['datetime']}")
if __name__ == "__main__":
main()
4、運行結果

5、一些問題
如何在xshell中運行代碼,可能會彈出X11轉發(fā)請求的窗口 ,根據(jù)提示關閉就行
