我刚开始了解scrapy。现在我正在尝试通过以下教程进行爬行。但我很难从 div 中抓取文本。
这是 items.py
from scrapy.item import Item, Fied
class DmozItem(Item):
name = Field()
title = Field()
pass
这是 dmoz_spider.py
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from dmoz.items import DmozItem
class DmozSpider(BaseSpider):
name = "dmoz"
allowed_domains = ["roxie.com"]
start_urls = ["http://www.roxie.com/events/details.cfm?eventID=4921702B-9E3D-8678-50D614177545A594"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
sites = hxs.select('//div[@id="eventdescription"]')
items = []
for site in sites:
item = DmozItem()
item['name'] = hxs.select("text()").extract()
items.append(item)
return items
现在我正在尝试通过以下命令从顶级文件夹中抓取:
scrapy crawl dmoz -o scraped_data.json -t json
但该文件仅使用“[”创建。
它在控制台中完美地工作(通过命令每个选择),但不知何故它不能作为脚本工作。我真的是scrapy的初学者。你们能告诉我如何获取“div”中的数据吗?提前致谢。
*另外,这是我得到的。
2013-06-19 08:43:56-0700 [scrapy] INFO: Scrapy 0.16.5 started (bot: dmoz)
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/twisted/web/microdom.py:181: SyntaxWarning: assertion is always true, perhaps remove parentheses?
assert (oldChild.parentNode is self,
2013-06-19 08:43:56-0700 [scrapy] DEBUG: Enabled extensions: FeedExporter, LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2013-06-19 08:43:56-0700 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-06-19 08:43:56-0700 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-06-19 08:43:56-0700 [scrapy] DEBUG: Enabled item pipelines:
2013-06-19 08:43:56-0700 [dmoz] INFO: Spider opened
2013-06-19 08:43:56-0700 [dmoz] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-06-19 08:43:56-0700 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-06-19 08:43:56-0700 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-06-19 08:43:56-0700 [dmoz] DEBUG: Crawled (200) <GET http://www.roxie.com/events/details.cfm?eventID=4921702B-9E3D-8678-50D614177545A594> (referer: None)
2013-06-19 08:43:56-0700 [dmoz] INFO: Closing spider (finished)
2013-06-19 08:43:56-0700 [dmoz] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 281,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 27451,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2013, 6, 19, 15, 43, 56, 569164),
'log_count/DEBUG': 7,
'log_count/INFO': 4,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2013, 6, 19, 15, 43, 56, 417661)}
2013-06-19 08:43:56-0700 [dmoz] INFO: Spider closed (finished)