【问题标题】:How to Data Scrape from multiple pages如何从多个页面抓取数据
【发布时间】:2021-07-12 15:21:56
【问题描述】:
import os
from webdriver_manager.chrome import ChromeDriverManager
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--start-maximized')
options.page_load_strategy = 'eager'

driver = webdriver.Chrome(options=options)
url = "https://www.moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24#MS24"
driver.get(url)
wait = WebDriverWait(driver, 20)

我想找到现金 EPS 的值(独立的和合并的),但主要问题是,页面上只有 5 个值,并且使用箭头按钮检索其他值直到结束。

如何一次性检索这些值?

【问题讨论】:

  • 按下那个按钮怎么样?可能不能,除非它们已经加载或者您可以访问他们的数据库,这使得该方法首先无用
  • 是的,但是如何知道最终值何时存在,因为该页面上仍然存在按钮。
  • @Matiiss 不,无权访问数据库,所以只需要从网页上报废
  • 好好按下按钮,直到你不再得到任何值
  • 这就是我想知道的如何知道它的紧迫性,因为它不会在最后一个值之后加载任何页面

标签: python python-3.x python-2.7 web-scraping selenium-chromedriver


【解决方案1】:

将我的评论进一步带到代码中。 评论: 这是一个分页元素,它的 href 为“javascript:void();”一旦点击超过分页计数。如果数据仍然存在,则它有一个分页#号(在这种情况下参见 4)。 moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24/…。所以任何一种条件都可以用于退出!

代码中的注释指的是建议。

df_list=pd.read_html(driver.page_source) # read the table through pandas
result=df_list[0] #load the result, which will be eventually appended for next pages.

current_page=driver.find_element_by_class_name('nextpaging') # find elment of span 
while True:
    current_page.click()
    time.sleep(20) # sleep for 20 
    current_page=driver.find_element_by_class_name('nextpaging')
    paging_link = current_page.find_element_by_xpath('..') # get the parent of this span which has the href
    print(f"Currentl url : { driver.current_url } Next paging link : { paging_link.get_attribute('href')} ")
    if "void" in paging_link.get_attribute('href'):
        print(f"Time to exit {paging_link.get_attribute('href')}")
        break # exit rule 

    df_list=pd.read_html(driver.page_source)
    result=result.append(df_list[0]) # append the result
   

【讨论】:

    【解决方案2】:

    基于在浏览此景点时查看 URL

    https://www.moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24/1#MS24
    

    箭头似乎导航到一个新的 URL,增加了 URL 中 # 符号前面的数字。

    所以,浏览页面看起来像这样:

    Page1: https://www.moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24/1#MS24
    Page2: https://www.moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24/2#MS24
    Page3: https://www.moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24/3#MS24
    etc...
    

    这些单独的网址可用于浏览此特定网站。可能这会工作

    def get_pg_url(pgnum):
        return 'https://www.moneycontrol.com/financials/marutisuzukiindia/ratiosVI/MS24/{}#MS24'.format(pgnum)
    

    网页抓取需要调整以适应目标视线。我输入了pgnum=10000,结果显示文本Data Not Available for Key Financial Ratios。当没有剩余页面时,您可能可以通过此文本告诉您。

    【讨论】:

    • 这就是我的问题,如何知道下一页加载已经结束。会试试这个,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多