2016-08-01 1 views
0

視差スクロールを実装したpageにアクセスしています。私は下部をスクロールするコードを使用していますが、BeautifulSoupは更新されたDOMを取得していません。コードは以下のとおりである:Python Selenium:スクロールダウンした後に更新されたHTML DOMを取得するには?

import requests 
from bs4 import BeautifulSoup 
from gensim.summarization import summarize 

from selenium import webdriver 
from datetime import datetime 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.keys import Keys 
from time import sleep 
import sys 
import os 
import xmltodict 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
import traceback 
import random 

driver = None 
driver = webdriver.Firefox() 
driver.maximize_window() 
def fetch_links(tag): 
    links = [] 
    url = 'https://steemit.com/trending/'+tag 
    driver.get(url) 
    html = driver.page_source 
    sleep(4) 

    soup = BeautifulSoup(html,'lxml') 
    entries = soup.select('.entry-title > a') 
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 
    sleep(5) 
    entries = soup.select('.entry-title > a') 
    for e in entries: 
     if e['href'].strip() not in entries: 
      links.append(e['href']) 
    return links 

答えて

2

おそらく、ウィンドウがスクロールされたら、ページを解析する必要があります。

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 

sleep(5) 

soup = BeautifulSoup(driver.page_source, 'lxml') 
entries = soup.select('.entry-title > a') 
+0

これは私も試したものですが、何の変化 – Volatil3

+0

問題は 'であると思われていませんBeautifulSoup'。すべてのタイトルは 'driver.page_source'によって返されたhtmlにあります。 –

+0

デフォルトでは、ページあたり20個のレコードが選択され、スクロールでは次の20個が選択されます。 – Volatil3

関連する問題