用于Javascript的Python爬虫?

1 投票
3 回答
3341 浏览
提问于 2025-04-15 23:16

有没有人能推荐一个好的Python网页抓取库,专门用来处理JavaScript代码的?希望这个库有好的文档和教程。我想看看有哪些选择,但最希望找到一个最简单、学习最快的……想知道有没有人有经验。我听说过spidermonkey,但也许还有更好的选择?

具体来说,我现在用BeautifulSoup和Mechanize达到了这个阶段,但我需要一种方法来打开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 个回答

1

你还可以使用一个叫做Spynner的“程序化网页浏览器”。我觉得这是最好的解决方案,使用起来相对简单。

1

我使用Python的webkit库来渲染基本的JavaScript,还用Chickenfoot来处理更复杂的互动。想了解更多信息,可以看看这个webkit的例子

1

我通常会在这种情况下自动操作一个真实的浏览器,然后从中获取处理后的HTML内容。

编辑:

这里有一个例子,展示了如何自动操作InternetExplorer,访问一个网址,并在页面加载后获取标题和地址。

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

撰写回答