【问题标题】:Python scraping using scrapy使用 scrapy 进行 Python 抓取
【发布时间】:2015-03-17 07:27:08
【问题描述】:

所以,我已经看到了如何使用 scrapy 的教程,现在我可以访问给定页面中的链接。但我想做的是给定一个页面,我想收集它的数据(元数据和摘要),我也想访问该页面中的链接并收集它们的数据。这是我目前的代码(尚未收集数据)

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.spider import BaseSpider
from scrapy.http import Request
from scrapy.selector import HtmlXPathSelector
from scrapy.selector import Selector
#from scrapy.item import SpideyItem

class spidey (CrawlSpider):
    name = "spidey"
    allowed_domains = ["wikipedia.org"]
    start_urls = ["http://en.wikipedia.org/wiki/Game_of_Thrones"]

    rules = (

        Rule(SgmlLinkExtractor(restrict_xpaths=('//div[@class="nw-  body"]//a/@href'))),
    Rule(SgmlLinkExtractor(allow=("http://en.wikipedia.org/wiki/",)), callback = 'parse_item'),

    )

def parse_item(self, response):
    sel = HtmlXPathSelector(response)
    print sel.xpath('//h1[@class="firstHeading"]/span/text()').extract()

因此,在此之后,我想收集初始页面的数据和我访问的链接中存在的数据。我是网络蜘蛛的新手,欢迎任何指针。

【问题讨论】:

    标签: python-2.7 web-scraping web-crawler scrapy-spider


    【解决方案1】:

    我不确定你的问题到底是什么,但如果你问如何从多个页面收集数据并将其保存到一个项目中......这就是你的答案:

    https://github.com/darkrho/scrapy-inline-requests

    此外,如果您不想以内联方式执行此操作,您始终可以将您的项目存储在 request.meta 中,并在请求中将其发送到一个从页面中提取数据的函数的回调。

    查看这个答案:How can i use multiple requests and pass items in between them in scrapy python

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2016-11-30
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      相关资源
      最近更新 更多