python - 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()
解决方案
虽然我不太确定你想从中得到什么,但我设法从 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">
推荐阅读
- javascript - 在 Firestore 中更新没有 ID 的特定文档
- php - 如何仅从具有特定值的 curl(php) 中获取键名
- python - 有没有一种pythonic方法可以将int转换为字符串,然后连接形成单个字符串值
- wcf - 使用 .NET 5.0 将 HTTP 请求字段添加到 WCF Web 服务请求
- c++ - 不能使用“==”运算符比较两个向量
- windows - Windows Docker 容器服务未启动
- php - PHP 查询总是返回用户存在,但直接对数据库运行相同的查询不会产生任何结果?
- java - 在 android studio 中创建一个新的 JSONObject 返回一个空对象
- image - 如何将图像从 imageview 保存到手机存储?
- android - 在 Android 应用程序中,如何在活动电话呼叫后关闭应用程序?