From 7978237ed57eddf0cbb50c236c58243e5ed94899 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Fri, 10 Jun 2016 17:42:18 +0200 Subject: [PATCH 1/3] Add FAQ entry on using BeautifulSoup in spider callbacks --- docs/faq.rst | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/faq.rst b/docs/faq.rst index 5cd62710a..712f3b585 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -3,6 +3,8 @@ Frequently Asked Questions ========================== +.. _faq-scrapy-bs-cmp: + How does Scrapy compare to BeautifulSoup or lxml? ------------------------------------------------- @@ -24,6 +26,36 @@ comparing `jinja2`_ to `Django`_. .. _jinja2: http://jinja.pocoo.org/ .. _Django: https://www.djangoproject.com/ +How can I use Scrapy with BeautifulSoup? +---------------------------------------- + +As mentioned :ref:`above `, BeautifulSoup can be used +for parsing HTML responses in Scrapy callbacks. +You just have to feed the response's body into a ``BeautifulSoup`` object +and extract whatever data you need from it. + +Here's an example spider using ``lxml`` parser with BeautifulSoup API:: + + + from bs4 import BeautifulSoup + import scrapy + + + class ExampleSpider(scrapy.Spider): + name = "example" + allowed_domains = ["example.com"] + start_urls = ( + 'http://www.example.com/', + ) + + def parse(self, response): + soup = BeautifulSoup(response.text, 'lxml') + yield { + "url": response.url, + "title": soup.h1.string + } + + .. _faq-python-versions: What Python versions does Scrapy support? From 6cbd92fac159672977239b4928c1cce15ba8b37a Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Tue, 14 Jun 2016 15:33:34 +0200 Subject: [PATCH 2/3] Add note on how to choose parser with BeautifulSoup --- docs/faq.rst | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/faq.rst b/docs/faq.rst index 712f3b585..af6103828 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -26,15 +26,17 @@ comparing `jinja2`_ to `Django`_. .. _jinja2: http://jinja.pocoo.org/ .. _Django: https://www.djangoproject.com/ -How can I use Scrapy with BeautifulSoup? ----------------------------------------- +Can I use Scrapy with BeautifulSoup? +------------------------------------ -As mentioned :ref:`above `, BeautifulSoup can be used +Yes, you can. +As mentioned :ref:`above `, `BeautifulSoup`_ can be used for parsing HTML responses in Scrapy callbacks. You just have to feed the response's body into a ``BeautifulSoup`` object and extract whatever data you need from it. -Here's an example spider using ``lxml`` parser with BeautifulSoup API:: +Here's an example spider using BeautifulSoup API, with ``lxml`` as the HTML parser +(so you get the same parsing speed as with scrapy/parsel selectors):: from bs4 import BeautifulSoup @@ -49,12 +51,19 @@ Here's an example spider using ``lxml`` parser with BeautifulSoup API:: ) def parse(self, response): + # use lxml to get decent HTML parsing speed soup = BeautifulSoup(response.text, 'lxml') yield { "url": response.url, "title": soup.h1.string } +.. note:: + + ``BeautifulSoup`` supports several HTML/XML parsers. + See `BeautifulSoup's official documentation`_ on which ones are available. + +.. _BeautifulSoup's official documentation: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#specifying-the-parser-to-use .. _faq-python-versions: From 1ff9a4828ba4c43343e36b2f1935363c067e5c84 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Tue, 14 Jun 2016 19:33:56 +0200 Subject: [PATCH 3/3] Do not commit on any HTML parsing speed assumption with BS4+lxml --- docs/faq.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/faq.rst b/docs/faq.rst index af6103828..82e1f3422 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -35,8 +35,7 @@ for parsing HTML responses in Scrapy callbacks. You just have to feed the response's body into a ``BeautifulSoup`` object and extract whatever data you need from it. -Here's an example spider using BeautifulSoup API, with ``lxml`` as the HTML parser -(so you get the same parsing speed as with scrapy/parsel selectors):: +Here's an example spider using BeautifulSoup API, with ``lxml`` as the HTML parser:: from bs4 import BeautifulSoup