mirror of https://github.com/scrapy/scrapy.git
merged upstream
This commit is contained in:
commit
7235040936
|
|
@ -21,7 +21,7 @@ Requirements
|
|||
* `Twisted`_ 2.5.0, 8.0 or above (Windows users: you may need to install
|
||||
`pywin32`_ because of `this Twisted bug`_)
|
||||
|
||||
* `libxml2`_ (2.6.28 or above is recommended)
|
||||
* `libxml2`_ (versions prior to 2.6.28 are known to have problems parsing certain malformed HTML, and have also been reported to contain leaks, so 2.6.28 or above is highly recommended)
|
||||
|
||||
.. _Python: http://www.python.org
|
||||
.. _Twisted: http://twistedmatrix.com
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ To export all scraped items into a CSV file (with specific fields in headers lin
|
|||
|
||||
EXPORT_FORMAT = 'csv_headers'
|
||||
EXPORT_FILE = 'scraped_items_with_headers.csv'
|
||||
EXPORT_FILEDS = ['name', 'price', 'description']
|
||||
EXPORT_FIELDS = ['name', 'price', 'description']
|
||||
|
||||
File Export Pipeline settings
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
|||
|
|
@ -247,15 +247,6 @@ Example::
|
|||
|
||||
COMMANDS_SETTINGS_MODULE = 'mybot.conf.commands'
|
||||
|
||||
.. setting:: CONCURRENT_SPIDERS
|
||||
|
||||
CONCURRENT_SPIDERS
|
||||
------------------
|
||||
|
||||
Default: ``8``
|
||||
|
||||
Maximum number of spiders to scrape in parallel.
|
||||
|
||||
.. setting:: CONCURRENT_ITEMS
|
||||
|
||||
CONCURRENT_ITEMS
|
||||
|
|
@ -264,7 +255,26 @@ CONCURRENT_ITEMS
|
|||
Default: ``100``
|
||||
|
||||
Maximum number of concurrent items (per response) to process in parallel in the
|
||||
Item Processor (also known as the Item Pipeline).
|
||||
Item Processor (also known as the :ref:`Item Pipeline <topics-item-pipeline>`).
|
||||
|
||||
.. setting:: CONCURRENT_REQUESTS_PER_SPIDER
|
||||
|
||||
CONCURRENT_REQUESTS_PER_SPIDER
|
||||
------------------------------
|
||||
|
||||
Default: ``8``
|
||||
|
||||
Specifies how many concurrent (ie. simultaneous) requests will be performed per
|
||||
open spider.
|
||||
|
||||
.. setting:: CONCURRENT_SPIDERS
|
||||
|
||||
CONCURRENT_SPIDERS
|
||||
------------------
|
||||
|
||||
Default: ``8``
|
||||
|
||||
Maximum number of spiders to scrape in parallel.
|
||||
|
||||
.. setting:: COOKIES_DEBUG
|
||||
|
||||
|
|
@ -516,8 +526,7 @@ LOG_FILE
|
|||
|
||||
Default: ``None``
|
||||
|
||||
File name to use for logging output. If None, standard input (or error) will be
|
||||
used depending on the value of the LOG_STDOUT setting.
|
||||
File name to use for logging output. If None, standard error will be used.
|
||||
|
||||
.. setting:: LOG_LEVEL
|
||||
|
||||
|
|
@ -536,8 +545,9 @@ LOG_STDOUT
|
|||
|
||||
Default: ``False``
|
||||
|
||||
If enabled logging will be sent to standard output, otherwise standard error
|
||||
will be used.
|
||||
If ``True``, all standard output (and error) of your process will be redirected
|
||||
to the log. For example if you ``print 'hello'`` it will appear in the Scrapy
|
||||
log.
|
||||
|
||||
.. setting:: MAIL_FROM
|
||||
|
||||
|
|
@ -725,16 +735,6 @@ A dict containing the request download handlers enabled by default in Scrapy.
|
|||
You should never modify this setting in your project, modify
|
||||
:setting:`REQUEST_HANDLERS` instead.
|
||||
|
||||
.. setting:: CONCURRENT_REQUESTS_PER_SPIDER
|
||||
|
||||
CONCURRENT_REQUESTS_PER_SPIDER
|
||||
------------------------------
|
||||
|
||||
Default: ``8``
|
||||
|
||||
Specifies how many concurrent (ie. simultaneous) requests will be performed per
|
||||
open spider.
|
||||
|
||||
.. setting:: REQUESTS_QUEUE_SIZE
|
||||
|
||||
REQUESTS_QUEUE_SIZE
|
||||
|
|
|
|||
|
|
@ -169,6 +169,31 @@ Let's see an example::
|
|||
|
||||
SPIDER = MySpider()
|
||||
|
||||
Another example returning multiples Requests and Items from a single callback::
|
||||
|
||||
from scrapy.selector import HtmlXPathSelector
|
||||
from scrapy.spider import BaseSpider
|
||||
from scrapy.http import Request
|
||||
from myproject.items import MyItem
|
||||
|
||||
class MySpider(BaseSpider):
|
||||
domain_name = 'http://www.example.com'
|
||||
start_urls = [
|
||||
'http://www.example.com/1.html',
|
||||
'http://www.example.com/2.html',
|
||||
'http://www.example.com/3.html',
|
||||
]
|
||||
|
||||
def parse(self, response):
|
||||
hxs = HtmlXPathSelector(response)
|
||||
for h3 in hxs.select('//h3').extract():
|
||||
yield MyItem(title=h3)
|
||||
|
||||
for url in hxs.select('//a/@href').extract():
|
||||
yield Request(url, callback=self.parse)
|
||||
|
||||
SPIDER = MySpider()
|
||||
|
||||
.. module:: scrapy.contrib.spiders
|
||||
:synopsis: Collection of generic spiders
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,11 @@ if sys.version_info < (2,5):
|
|||
# monkey patches to fix external library issues
|
||||
from scrapy.xlib import twisted_250_monkeypatches
|
||||
|
||||
# add some common encoding aliases not included by default in Python
|
||||
from scrapy.utils.encoding import add_encoding_alias
|
||||
add_encoding_alias('gb2312', 'zh-cn')
|
||||
add_encoding_alias('cp1251', 'win-1251')
|
||||
|
||||
# optional_features is a set containing Scrapy optional features
|
||||
optional_features = set()
|
||||
|
||||
|
|
|
|||
|
|
@ -20,10 +20,10 @@ class SpiderInfo(object):
|
|||
|
||||
def __init__(self, download_delay=None, max_concurrent_requests=None):
|
||||
if download_delay is None:
|
||||
self.download_delay = settings.getint('DOWNLOAD_DELAY')
|
||||
self.download_delay = settings.getfloat('DOWNLOAD_DELAY')
|
||||
else:
|
||||
self.download_delay = download_delay
|
||||
if download_delay:
|
||||
if self.download_delay:
|
||||
self.max_concurrent_requests = 1
|
||||
elif max_concurrent_requests is None:
|
||||
self.max_concurrent_requests = settings.getint('CONCURRENT_REQUESTS_PER_SPIDER')
|
||||
|
|
|
|||
|
|
@ -137,7 +137,8 @@ class ExecutionEngine(object):
|
|||
if request:
|
||||
dwld = mustbe_deferred(self.download, request, spider)
|
||||
dwld.chainDeferred(deferred).addBoth(lambda _: deferred)
|
||||
dwld.addErrback(log.err, "Unhandled error on engine._next_request")
|
||||
dwld.addErrback(log.err, "Unhandled error on engine._next_request()",
|
||||
spider=spider)
|
||||
return dwld
|
||||
|
||||
def spider_is_idle(self, spider):
|
||||
|
|
@ -173,7 +174,7 @@ class ExecutionEngine(object):
|
|||
# next core refactoring.
|
||||
#schd.addErrback(log.err, "Error on engine.crawl()")
|
||||
schd.addBoth(self.scraper.enqueue_scrape, request, spider)
|
||||
schd.addErrback(log.err, "Unhandled error on engine.crawl()")
|
||||
schd.addErrback(log.err, "Unhandled error on engine.crawl()", spider=spider)
|
||||
schd.addBoth(lambda _: self.next_request(spider))
|
||||
|
||||
def schedule(self, request, spider):
|
||||
|
|
|
|||
|
|
@ -186,7 +186,8 @@ class Scraper(object):
|
|||
if isinstance(spider_failure.value, IgnoreRequest):
|
||||
return
|
||||
elif spider_failure is propagated_failure:
|
||||
log.err(spider_failure, 'Unhandled error propagated to spider and wasn\'t handled')
|
||||
log.err(spider_failure, 'Unhandled error propagated to spider', \
|
||||
spider=spider)
|
||||
return # stop propagating this error
|
||||
else:
|
||||
return spider_failure # exceptions raised in the spider code
|
||||
|
|
|
|||
|
|
@ -87,7 +87,11 @@ def err(_stuff=None, _why=None, **kwargs):
|
|||
domain = kwargs.pop('domain', None)
|
||||
spider = kwargs.pop('spider', None)
|
||||
component = kwargs.pop('component', BOT_NAME)
|
||||
kwargs['system'] = domain or spider.domain_name if spider else component
|
||||
if domain is not None:
|
||||
import warnings
|
||||
warnings.warn("'domain' argument of scrapy.log.err() is deprecated, " \
|
||||
"use 'spider' argument instead", DeprecationWarning, stacklevel=2)
|
||||
kwargs['system'] = domain or (spider.domain_name if spider else component)
|
||||
if _why:
|
||||
_why = unicode_to_str("ERROR: %s" % _why)
|
||||
log.err(_stuff, _why, **kwargs)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
import unittest
|
||||
|
||||
import scrapy # adds encoding aliases (if not added before)
|
||||
|
||||
class EncodingAliasesTestCase(unittest.TestCase):
|
||||
|
||||
def test_encoding_aliases(self):
|
||||
"""Test common encdoing aliases not included in Python"""
|
||||
|
||||
uni = u'\u041c\u041e\u0421K\u0412\u0410'
|
||||
str = uni.encode('windows-1251')
|
||||
self.assertEqual(uni.encode('windows-1251'), uni.encode('win-1251'))
|
||||
self.assertEqual(str.decode('windows-1251'), str.decode('win-1251'))
|
||||
|
||||
text = u'\u8f6f\u4ef6\u540d\u79f0'
|
||||
str = uni.encode('gb2312')
|
||||
self.assertEqual(uni.encode('gb2312'), uni.encode('zh-cn'))
|
||||
self.assertEqual(str.decode('gb2312'), str.decode('zh-cn'))
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
import codecs
|
||||
|
||||
def add_encoding_alias(encoding, alias, overwrite=False):
|
||||
try:
|
||||
codecs.lookup(alias)
|
||||
alias_exists = True
|
||||
except LookupError:
|
||||
alias_exists = False
|
||||
if overwrite or not alias_exists:
|
||||
codec = codecs.lookup(encoding)
|
||||
codecs.register(lambda x: codec if x == alias else None)
|
||||
Loading…
Reference in New Issue