【发布时间】: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