如何使用 Scrapy 遍历网站?我想提取所有匹配的网站的正文http://www.saylor.org/site/syllabus.php?cid=NUMBER
,其中 NUMBER 是 1 到 400 左右。
我写了这个蜘蛛:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from syllabi.items import SyllabiItem
class SyllabiSpider(CrawlSpider):
name = 'saylor'
allowed_domains = ['saylor.org']
start_urls = ['http://www.saylor.org/site/syllabus.php?cid=']
rules = [Rule(SgmlLinkExtractor(allow=['\d+']), 'parse_syllabi')]
def parse_syllabi(self, response):
x = HtmlXPathSelector(response)
syllabi = SyllabiItem()
syllabi['url'] = response.url
syllabi['body'] = x.select("/html/body/text()").extract()
return syllabi
但它不起作用。我知道它正在该 start_url 中寻找链接,这并不是我真正想要的。我想遍历这些站点。说得通?
谢谢您的帮助。