scrapy - 如何从'div'获取文本

时间:2013-06-18 23:05:29

标签: html text scrapy web-crawler

我刚刚开始了解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)

2 个答案:

答案 0 :(得分:7)

我可以为你提供电影的标题,但是我对XPath有些蹩脚,所以描述XPath会让你在<div class="tabbertab" title="Synopsis">元素中找到所有。这不是理想的,但它是一个起点。获取图像URL留作OP的练习。 :)

from scrapy.item import Item, Fied
class DmozItem(Item):
    title = Field()
    description = Field()
    pass

class DmozSpider(BaseSpider):
    name = "test"
    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)
        item = DmozItem()
        item['title'] = hxs.select('//div[@style="width: 100%;"]/text()').extract()
        item['description'] = hxs.select('//div[@class="tabbertab"]').extract()
        return item

答案 1 :(得分:3)

只需替换

item['name'] = hxs.select("text()").extract()

item['name'] = site.select("text()").extract()

希望有所帮助。