在ubuntu 20.04上使用selinium headless模式爬取網(wǎng)頁

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ù)提示關閉就行


最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容