使用 Python 下载 URL 的 html - 但启用了 javascript
我正在尝试下载此页面,以便我可以抓取搜索结果。但是,当我下载页面并尝试使用 BeautifulSoup 处理它时,我发现页面的某些部分(例如搜索结果)未包含在内,因为网站检测到未启用 javascript。
有没有办法在Python中启用JavaScript来下载URL的HTML?
I am trying to download this page so that I can scrape the search results. However, when I download the page and try to process it with BeautifulSoup, I find that parts of the page (for example, the search results) aren't included as the site has detected that javascript is not enabled.
Is there a way to download the HTML of a URL with javascript enabled in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@kstruct:我的首选方法是使用已经编写的浏览器,而不是使用 QtWebKit 和 PyQt4 编写完整的浏览器。有 PhantomJS (C++) 项目,或 PyPhantomJS (Python)。基本上,Python 是 QtWebKit 和 Python。
它们都是无头浏览器,您可以直接通过 JavaScript 进行控制。 Python 版本有一个插件系统,允许您扩展核心,以允许您需要的附加功能。
这是 PyPhantomJS 的示例脚本(使用 saveToFile 插件 )
有用的链接:
API 参考 | 如何编写插件
@kstruct: My preferred way, instead of writing a full browser with QtWebKit and PyQt4, is to use one already written. There's the PhantomJS (C++) project, or PyPhantomJS (Python). Basically the Python one is QtWebKit and Python.
They're both headless browsers which you can control directly from JavaScript. The Python version has a plug-in system which allows you to extend the core too, to allow additional functionalities should you need.
Here's an example script for PyPhantomJS (with the saveToFile plugin)
Useful links:
API reference | How to write plugins
我会考虑使用 PyQt4 库中的 QtWebKit 模块。该模块将使 JS 代码在页面上运行,一旦完成,您可以使用我相信的标准方法保存 HTML。
否则,Selenium 就是最佳选择。它允许您通过 Python 脚本控制 Web 浏览器来拉出页面,然后提取所有 DOM 内容。
I'd look into using the QtWebKit module in the PyQt4 library. The module will let the JS code run on the page and once it's done, you can save the HTML using standard methods I believe.
Otherwise, Selenium is the way to go. It lets you control a web browser from your Python script to pull up the page and then extract all the DOM stuff.
一旦您想要启用 JavaScript,您所要求的就非常接近浏览器了。您可以使用 jython,然后使用 HtmlUnit,它是一个基于 java 的无头浏览器。它相当快,但不太稳定(因为它模仿浏览器,但并不是真正的浏览器)。我认为最快、最简单的方法是使用selenium(ide或最好是rc)。 Selenium 使您能够控制您最喜欢的浏览器(FF、IE、chrome...)。尽管它是用于测试目的,但它可能对您有用。它稳定且速度相当快(我认为它甚至比 HtmlUnit 更快)。
Once you wanta javascript enabled, what you're asking for is very close to a browser. You can use jython and then use HtmlUnit, which is a headless java based browser. It's pretty fast but not very stable (because is imitates a browser and isn't really a browser). I think the fastest and easiest way is to use selenium (ide or preferably rc). Selenium gives you the ability to control your favorite browser (FF, IE, chrome,..). Although it's meant for testing puposes, it'll probably work for you. It's stable and pretty fast (I think it's even faster than HtmlUnit).
您可以在 http://htql.net 上使用 htql。
顺便说一句,您需要在 http://irobotsoft.com/ 安装 IRobot
You can use htql at http://htql.net.
BTW, you will need to install IRobot at http://irobotsoft.com/