首页 > 解决方案 > Python Selenium 和 BS

问题描述

我一直在尝试从一个页面中获取一个元素,我单击该元素以使用soup.find_all 进入下一页。问题是它给了我第一页的元素。提前致谢。

编码:

driver = webdriver.Chrome("C:/Users/user/PycharmProjects/Graph/chromedriver.exe")
driver.get("https://blockchain.coinmarketcap.com/chain/bitcoin")
time.sleep(2)


next_page = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, next_page_css)))

time.sleep(0.1)

actions = ActionChains(driver)
actions.move_to_element(next_page).perform()

next_page.click()
time.sleep(0.1)
content = driver.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content, "html.parser")

stats = str(soup.find_all('tbody', {"class": "ant-table-tbody"}))
print(stats)
driver.quit()

网站:https ://blockchain.coinmarketcap.com/chain/bitcoin

我点击按钮的照片

标签: pythonseleniumweb-scrapingbeautifulsoup

解决方案


虽然我不太确定你想从中得到什么,但我设法从 page2 获取数据。

据我所知,似乎有 2 个问题:1)睡眠时间不足以加载页面 2)必须更改 CSS 选择器,因为它不起作用

next_page = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".ant-pagination-item-2")))

time.sleep(2)

actions = ActionChains(driver)
actions.move_to_element(next_page).perform()

next_page.click()
time.sleep(2)
content = driver.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content, "html.parser")

stats = str(soup.find_all('tbody', {"class": "ant-table-tbody"}))
print(stats)

回来 :

<tbody class="ant-table-tbody"><tr class="ant-table-row ant-table-row-level-0" data-row-key="0000000000000000000aae7c345c1b0acde7650e5448ca5dfc6c8de7d415ed22">

推荐阅读