【问题标题】:Python Scraper for Javascript?用于 Javascript 的 Python Scraper?
【发布时间】:2010-05-28 02:38:00
【问题描述】:

谁能指导我使用一个好的 Python 屏幕抓取库,用于 JavaScript 代码(希望有好的文档/教程)?我想看看那里有哪些选择,但最容易学习并获得最快的结果......想知道是否有人有经验。我听说过一些关于蜘蛛猴的东西,但也许还有更好的?

具体来说,我使用 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 来实现它。谢谢!

【问题讨论】:

    标签: javascript python screen-scraping


    【解决方案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
    

    【讨论】:

    • 这个和selenium类似吗?我尝试过以这种方式自动化,但在生成的 python 源代码时遇到了一些问题。我需要关注这种类型的所有 javascript 链接并从每个链接下载/解析数据
    • 我只是直接自动化浏览器。在 Windows 上,您可以使用 Internet Explorer 执行此操作,或使用 WebKit 以跨平台方式执行此操作。
    • 我认为 Selenium (seleniumhq.org) 和 Firefox 是 Linux 的发展方向。
    【解决方案2】:

    我使用 Python 绑定到 webkit 来呈现基本的 JavaScript,并使用Chickenfoot 进行更高级的交互。请参阅this webkit example 了解更多信息。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2015-02-26
        • 1970-01-01
        • 2015-08-16
        • 2011-06-17
        • 2021-10-19
        • 2013-03-05
        • 2021-12-09
        • 1970-01-01
        • 2021-10-29
        相关资源
        最近更新 更多