我想从下面的链接中删除一些数据:
category=svsp
我的目标只是检索data.frame中所有仪器的表(显示在页面1、2、3等的“搜索结果”中)。我不能简单地使用urllib和urllib2来检索静态数据,因为我需要通过剪裁按钮来模仿人类:Ghost或Selenium是最好的选择。
但是,我真的不知道如何翻译成代码“点击第2页”,“点击第3页”.以及得到总页数。
我的代码:
from ghost import Ghost
url = "http://www.six-structured-products.com/en/search-find/new-search#search_type=profi&class_category=svsp"
gh = Ghost()
page, resources = gh.open(url)我被困在那里,不知道该放哪个标识符,而不知道XXX:
page, resources = ghost.evaluate(
"document.getElementById(XXX).click();", expect_loading=True)(我也会接受使用Selenium的解决方案)
发布于 2015-01-09 20:17:24
您还可以这样使用next按钮:
import logging
import sys
from ghost import Ghost, TimeoutError
logging.basicConfig(level=logging.INFO)
url = "http://www.six-structured-products.com/en/search-find/new-search#search_type=profi&class_category=svsp"
ghost = Ghost(wait_timeout=20, log_level=logging.CRITICAL)
data = dict()
def extract_value(line, ntd):
return line.findFirst('td.DataItem:nth-child(%d)' % ntd).toPlainText()
def extract(ghost):
lines = ghost.main_frame.findAllElements(
'.derivativeSearchResult > tbody:nth-child(2) tr'
)
for line in lines:
symbol = extract_value(line, 2)
name = extract_value(line, 5)
logging.info("Found %s: %s" % (symbol, name))
# Persist data here
ghost.sleep(1)
try:
ghost.click('.pagination_next a', expect_loading=True)
except TimeoutError:
sys.exit(0)
extract(ghost)
ghost.open(url)
extract(ghost)发布于 2014-12-14 19:41:29
做一个无止境的循环来增加页面索引。当您找不到带有当前索引的按钮时,退出循环:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Firefox()
driver.get('http://www.six-structured-products.com/en/search-find/new-search#search_type=profi&class_category=svsp')
page = 2 # starting page
while True:
try:
button = driver.find_element_by_xpath('//ul[@id="pagination_pages"]/li[@class="pagination_page" and . = "%d"]' % page)
except NoSuchElementException:
break
time.sleep(1)
button.click()
page += 1
print page # total number of pages
driver.close()注意,与time.sleep()不同,更可靠的方法是使用等待。
https://stackoverflow.com/questions/27473156
复制相似问题