首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Ghost刮取数据

用Ghost刮取数据
EN

Stack Overflow用户
提问于 2014-12-14 19:27:01
回答 2查看 1.8K关注 0票数 0

我想从下面的链接中删除一些数据:

category=svsp

我的目标只是检索data.frame中所有仪器的表(显示在页面1、2、3等的“搜索结果”中)。我不能简单地使用urlliburllib2来检索静态数据,因为我需要通过剪裁按钮来模仿人类:GhostSelenium是最好的选择。

但是,我真的不知道如何翻译成代码“点击第2页”,“点击第3页”.以及得到总页数。

我的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
page, resources = ghost.evaluate(
"document.getElementById(XXX).click();", expect_loading=True)

(我也会接受使用Selenium的解决方案)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-09 20:17:24

您还可以这样使用next按钮:

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2014-12-14 19:41:29

做一个无止境的循环来增加页面索引。当您找不到带有当前索引的按钮时,退出循环:

代码语言:javascript
复制
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()不同,更可靠的方法是使用等待

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27473156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档