2017-09-28 26 views
0

私たちはページの一番下まで来ると、製品ラインごとに "www.jabong.com"というウェブサイトをスクラップしようとしています。私はすべてのリンクを破棄したい。私が試しているコードは次のとおりです。python seleniumが自動的にページを追加します

from time import sleep 
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 


def fetch_links(url, product_line_name): 

    chrome_options = Options() 
    chrome_options.add_argument("--disable-notifications") 
    chrome_path = r"D:\chromedriver.exe" 
    driver = webdriver.Chrome(chrome_path, chrome_options=chrome_options) 
    driver.get(url) 
    button="load-more-products" 
    while True: 
     element=driver.find_element_by_class_name(button).click() 
     driver.execute_script("arguments[0].scrollIntoView();", element) 



link_list=["https://www.jabong.com/women/clothing/trousers-jeans/trousers/?source=topnav_women"] 
product_line=["trousers"] 

fetch_links(link_list[0],product_line[0]) 

ここで問題が見つかりません。私はclass_name、css_selectorでも試しましたが、それらのどれもが動作していないようです。ブラウザに自動的に製品をロードしたい

+0

これは、これを行うための適切なアプローチではありません。スクロールすると新しい製品が表示され、これを続けるとページのデータが大きくなりすぎてクロムとセレンが遅すぎて処理できなくなるためです。それで、これでScrapyを使う方法を見つけて、セレンを使わないでください –

+0

フレーム内の要素ですか? htmlを投稿してください。 – IamBatman

答えて

0

これを試してください。必要なデータを取得します。

import time 
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.keys import Keys 

link_list = ["https://www.jabong.com/women/clothing/trousers-jeans/trousers/?source=topnav_women",] 

def fetch_links(link): 
    chrome_options = Options() 
    chrome_options.add_argument("--disable-notifications") 
    driver = webdriver.Chrome(chrome_options=chrome_options) 
    driver.get(link) 
    actions = ActionChains(driver) 
    for _ in range(10): #Adjust this range according to your need, I meant how far you wanna go down. 
     actions.send_keys(Keys.SPACE).perform() 
     time.sleep(1) 

    for item in driver.find_elements_by_css_selector(".h4"): 
     print(item.text) 

fetch_links(link_list[0]) 

部分的な結果:

W Orange Printed Palazzo 
DOROTHY PERKINS Olive Solid Mid Rise Skinny Fit Coloured Pant 
VARANGA White Solid Palazzo 
VARANGA Blue Printed Palazzo 
関連する問題