用于Javascript的Python Scraper?

时间:2010-05-28 02:38:01

标签: javascript python screen-scraping

任何人都可以指导我使用一个优秀的Python屏幕抓取库来获取javascript代码(希望有一个很好的文档/教程)吗?我想看看有哪些选项,但最重要的是最容易学习的最快结果...想知道是否有人有经验。我听说过一些关于spidermonkey的东西,但也许有更好的那些?

具体来说,我使用BeautifulSoup和Mechanize来到这里,但需要一种方法来打开javascript弹出窗口,提交数据,并在javascript弹出窗口中下载/解析结果。

<a href="javascript:openFindItem(12510109)" onclick="s_objectID=&quot;javascript:openFindItem(12510109)_1&quot;;return this.s_oc?this.s_oc(e):true">Find Item</a>

我想通过Google App引擎和Django实现这一点。谢谢!

3 个答案:

答案 0 :(得分:1)

我通常做的是在这些情况下自动化实际的浏览器,并从那里获取已处理的HTML。

修改

这是一个自动化InternetExplorer导航到URL并在页面加载后获取标题和位置的示例。

from win32com.client import Dispatch

from ctypes import Structure, pointer, windll
from ctypes import c_int, c_long, c_uint
import win32con
import pywintypes

class POINT(Structure):
    _fields_ = [('x', c_long),
                ('y', c_long)]
    def __init__( self, x=0, y=0 ):
        self.x = x
        self.y = y

class MSG(Structure):
    _fields_ = [('hwnd', c_int),
                ('message', c_uint),
                ('wParam', c_int),
                ('lParam', c_int),
                ('time', c_int),
                ('pt', POINT)]

def wait_until_ready(ie):
    pMsg = pointer(MSG())
    NULL = c_int(win32con.NULL)

    while True:

        while windll.user32.PeekMessageW(pMsg, NULL, 0, 0, win32con.PM_REMOVE) != 0:
            windll.user32.TranslateMessage(pMsg)
            windll.user32.DispatchMessageW(pMsg)

        if ie.ReadyState == 4:
            break


ie = Dispatch("InternetExplorer.Application")

ie.Visible = True

ie.Navigate("http://google.com/")

wait_until_ready(ie)

print "title:", ie.Document.Title
print "location:", ie.Document.location

答案 1 :(得分:1)

我使用Python绑定到webkit来呈现基本JavaScript和Chickenfoot以进行更高级的交互。有关详细信息,请参阅this webkit example

答案 2 :(得分:1)

您还可以使用名为Spynner的“程序化Web浏览器”。我发现这是最好的解决方案。相对容易使用。