前段时间我设置了一个使用 selenium 的 Python 脚本来爬过一个网站。我试图采用代码从新网页获取数据,但遇到了麻烦。问题似乎是该页面是一个应用程序而不是一个更标准的页面(我的理解并不理想)。
我似乎要更新的元素是“ag-paging-number”。我尝试了 click() 而不是 page,但这也没有用。
最终,脚本应该循环遍历 1496 页并保存数据(任何关于如何设置目录的提示都很好)。任何帮助,将不胜感激。 显然,一旦脚本开始工作,我将切换到无头
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import codecs
options = Options()
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome()
url = "https://app.climatevaluation.com/apps/projections/table/index.html"
driver.get(url)
for page in range(1,1496,1):
time.sleep(10)
try:
driver.find_element_by_class_name("ag-paging-number").click()
except:
break
with codecs.open('session'+str(page)+'.htm', 'w','utf-8') as out:
out.write(driver.page_source)
我想你愿意点击下一页按钮吗?
使用
driver.find_element_by_xpath("//div[@ref='btNext']/span").click()
您可能还想在循环之前添加
driver.implicitly_wait(10)