Grabbed data from a given URL and put it into a file using scrapy

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 01:25:17

问题


I am trying to scrapped deeply a given web site and grab text from all over pages. I am using scrapy to scrap web site

here is how i am running spider scrapy crawl stack_crawler -o items.json

item.json file coming empty

Here is spider code_snap

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

#from tutorial.items import TutorialItem

from tutorial.items import DmozItem

class StackCrawlerSpider(CrawlSpider):
    name = 'stack_crawler'
    allowed_domains = ['http://www.dmoz.org']
    start_urls = ['http://www.dmoz.org/']

    rules = (
        Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        i = TutorialItem()
        i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
        i['name'] = response.xpath('//div[@id="name"]').extract()
        i['description'] = response.xpath('//div[@id="description"]').extract()
        return i

Here is the log that i getting when i am running spider to crawl

dummy-MacBook-Pro:spiders Dummy$ scrapy crawl stack_crawler -o items.json
2016-06-09 10:22:23 [scrapy] INFO: Scrapy 1.1.0 started (bot: tutorial)
2016-06-09 10:22:23 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'tutorial.spiders', 'FEED_URI': 'items.json', 'SPIDER_MODULES': ['tutorial.spiders'], 'BOT_NAME': 'tutorial', 'ROBOTSTXT_OBEY': True, 'FEED_FORMAT': 'json'}
2016-06-09 10:22:23 [scrapy] INFO: Enabled extensions:
['scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2016-06-09 10:22:23 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2016-06-09 10:22:23 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2016-06-09 10:22:23 [scrapy] INFO: Enabled item pipelines:
[]
2016-06-09 10:22:23 [scrapy] INFO: Spider opened
2016-06-09 10:22:23 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-06-09 10:22:23 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6024
2016-06-09 10:22:24 [scrapy] DEBUG: Crawled (200) <GET http://www.dmoz.org/robots.txt> (referer: None)
2016-06-09 10:22:24 [scrapy] DEBUG: Crawled (200) <GET http://www.dmoz.org/> (referer: None)
2016-06-09 10:22:24 [scrapy] INFO: Closing spider (finished)
2016-06-09 10:22:24 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 430,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 5694,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 6, 9, 4, 52, 24, 862900),
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2016, 6, 9, 4, 52, 23, 483092)}
2016-06-09 10:22:24 [scrapy] INFO: Spider closed (finished)

Item Code snap

import scrapy
class DmozItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()
    desc = scrapy.Field()

Can any one help me figure out what am i doing wrong at code level to get data.


回答1:


I think you are new to scrapy and you have made so many mistakes in your code

1.we have default functions parse or start_product_requests in scrapy, So you can avoid using LinkExtractor there. Use parse function and get the start_urls response directly there.

2.You have define one item in items.py and using another. So the field names are different, get a conflict there.

3.The path where you are taking values for fields are in correct.

You have to try this

spider code_snap

import scrapy

from lxml import html
from scrapy.spiders import CrawlSpider, Rule
from tutorial.items import DmozItem

class StackCrawlerSpider(CrawlSpider):
    name = 'stack_crawler'
    allowed_domains = ['http://www.dmoz.org']
    start_urls = ['http://www.dmoz.org/']

    def parse(self, response):  
        doc = html.fromstring(response.body)
        i = DmozItem()
        i['title'] = doc.xpath('//meta[@property="og:title"]/@content')
        i['link'] = response.url
        i['desc'] = doc.xpath('//meta[@name="description"]/@content')
        yield i

Item Code snap

import scrapy
class DmozItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()
    desc = scrapy.Field()

this works.




回答2:


dmoz.org does not have any links with "Items" in the href, so your rule is not finding any links and that is why your items.json file is empty.



来源:https://stackoverflow.com/questions/37717122/grabbed-data-from-a-given-url-and-put-it-into-a-file-using-scrapy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!