簡體   English   中英

Selenium - 從第一頁到最后一頁檢索 html

[英]Selenium - Retrieving html from first page until last page

我正在嘗試檢索網頁的 html,單擊下一步按鈕,然后重復該操作,直到到達最后一頁。 我想順便獲取所有文章的標題(h2),只設法檢索了其中的一部分。 這是我的代碼:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException

options = Options()

driver = webdriver.Chrome("C:/Users/krish/Desktop/chromedriver_win32/chromedriver.exe")
driver.get('https://www.cnbcindonesia.com/tag/pasar-modal')

while True:
    try:
        time.sleep(4)
        driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".icon.icon-angle-right"))))
    except NoSuchElementException:
        break

doc = driver.page_source

from bs4 import BeautifulSoup as bs

html = doc
soup = bs(html, 'html.parser')

for word in soup.find_all('h2'):
    find_all_title = word.get_text()
    print(find_all_title)

這是結果

Penerbitan Obligasi Korporasi di Kuartal I Capai Rp 30 T
Waskita Karya Terbitkan Obligasi Rp 3,45 Triliun
Baru IPO, Direksi dan Komisaris Kioson Mengundurkan Diri
Rekor Baru IHSG Berpotensi Pecah Lagi Hari Ini
Kelas BPJS Kesehatan Dihapus Juli, Iuran Barunya Jadi Segini?
Pemerintahan Israel Akan Dibubarkan, Apa yang Terjadi?
Massa Geruduk Rumah Yusuf Mansur Terkait Investasi Batu Bara
Harga Batu Bara Terbang 6% Lebih!
Mau Cuan? Coba Cermati Saham Pilihan Berikut Ini

如您所見,它只檢索幾個標題

謝謝!

遍歷https://www.cnbcindonesia.com/tag/pasar-modal/$var?kanal=&tipe=

您要廢棄的網站是分頁的,因此您需要遍歷頁面。 您不能只點擊主頁( https://www.cnbcindonesia.com/tag/pasar-modal )並獲取所有數據,因為某些數據是分頁的。

使用頁碼更改 $var 並將該頁面設置為您要廢棄的網絡鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM