Selenium 与 BeautifulSoup 的网页抓取 [英] Selenium versus BeautifulSoup for web scraping

查看:41
本文介绍了Selenium 与 BeautifulSoup 的网页抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 从网站上抓取内容.首先,我在 Python 上使用了 BeautifulSoupMechanize,但我看到该网站有一个通过 JavaScript 创建内容的按钮,所以我决定使用 Selenium.

I'm scraping content from a website using Python. First I used BeautifulSoup and Mechanize on Python but I saw that the website had a button that created content via JavaScript so I decided to use Selenium.

鉴于我可以使用 Selenium 和 driver.find_element_by_xpath 之类的方法找到元素并获取它们的内容,当我可以只使用 Selenium 时,有什么理由使用 BeautifulSoup一切?

Given that I can find elements and get their content using Selenium with methods like driver.find_element_by_xpath, what reason is there to use BeautifulSoup when I could just use Selenium for everything?

在这种特殊情况下,我需要使用 Selenium 来单击 JavaScript 按钮,那么使用 Selenium 解析更好还是应该同时使用 Selenium 和 Beautiful Soup?

And in this particular case, I need to use Selenium to click on the JavaScript button so is it better to use Selenium to parse as well or should I use both Selenium and Beautiful Soup?

推荐答案

在直接回答您的问题之前,作为起点值得说:如果您需要做的只是从静态 HTML 页面中提取内容,您可能应该使用HTTP 库(例如 Requests 或内置的 urllib.request) 和 lxmlBeautifulSoup,而不是Selenium(尽管Selenium 可能也足够了).不不必要地使用 Selenium 的优点:

Before answering your question directly, it's worth saying as a starting point: if all you need to do is pull content from static HTML pages, you should probably use a HTTP library (like Requests or the built-in urllib.request) with lxml or BeautifulSoup, not Selenium (although Selenium will probably be adequate too). The advantages of not using Selenium needlessly:

  • 带宽和运行脚本的时间.使用 Selenium 意味着获取您在浏览器中访问页面时通常会获取的所有资源 - 样式表、脚本、图像等.这可能是不必要的.
  • 稳定性和易于错误恢复.Selenium 可能有点脆弱,根据我的经验 - 即使使用 PhantomJS - 并且创建架构来杀死挂起的 Selenium 实例并创建一个新实例比在使用 时设置简单的异常重试逻辑更令人恼火请求.
  • 潜在的 CPU 和内存使用量 - 取决于您正在抓取的网站,以及您尝试并行运行的蜘蛛线程数量,可以想象 DOM 布局逻辑或 JavaScript 执行可能会变得非常昂贵.

请注意,需要 cookie 才能运行的站点并不是突破 Selenium 的理由——您可以轻松创建一个 URL 打开函数,使用 cookielib/cookiejar.

Note that a site requiring cookies to function isn't a reason to break out Selenium - you can easily create a URL-opening function that magically sets and sends cookies with HTTP requests using cookielib/cookiejar.

好的,那么您为什么要考虑使用 Selenium?几乎完全是为了处理您要抓取的内容通过 JavaScript 添加到页面而不是烘焙到 HTML 中的情况.即便如此,您也可以在不破坏重型机械的情况下获得所需的数据.通常适用以下场景之一:

Okay, so why might you consider using Selenium? Pretty much entirely to handle the case where the content you want to crawl is being added to the page via JavaScript, rather than baked into the HTML. Even then, you might be able to get the data you want without breaking out the heavy machinery. Usually one of these scenarios applies:

  • 与页面一起使用的 JavaScript 已将内容嵌入其中.JavaScript 只是用来做模板或其他将内容放入页面的 DOM 操作.在这种情况下,您可能想看看是否有一种简单的方法可以使用正则表达式直接从 JavaScript 中提取您感兴趣的内容.
  • JavaScript 正在访问 Web API 以加载内容.在这种情况下,请考虑是否可以识别相关的 API URL 并自行点击它们;这可能比实际运行 JavaScript 和从网页上抓取内容更简单、更直接.

如果您确实决定您的情况适合使用 Selenium,请在无头模式下使用它,(至少)Firefox 和 Chrome 驱动程序支持这种模式.网络爬虫通常不需要实际以图形方式呈现页面,或使用任何特定于浏览器的怪癖或功能,因此无头浏览器 - 具有较低的 CPU 和内存成本以及较少的移动部件会崩溃或挂起 - 是理想的选择.

If you do decide your situation merits using Selenium, use it in headless mode, which is supported by (at least) the Firefox and Chrome drivers. Web spidering doesn't ordinarily require actually graphically rendering the page, or using any browser-specific quirks or features, so a headless browser - with its lower CPU and memory cost and fewer moving parts to crash or hang - is ideal.

这篇关于Selenium 与 BeautifulSoup 的网页抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆