我正在尝试使用scrapy抓取整个网站 . 根据scarpy的文档
start_urls - 当没有指定特定URL时,蜘蛛将开始爬网的URL列表 . 因此,下载的第一页将是此处列出的页面 . 后续URL将从起始URL中包含的数据连续生成 .
所以根据这个定义,scrapy应该通过 start_urls
下提到的页面上的所有子URL进行蜘蛛,但它只会抓取我指定的url . 我确实指定了Scrapy - Crawl whole website中提到的规则,但它没有帮助 . 它只会抓取并输出我在start_urls中指定的页面 .
这是我的代码片段:
class AcdivocaFirstSpider(scrapy.Spider):
name = "example_sample"
allowed_domains = ["example.org"]
start_urls = ["http://www.example.org/site/id/home"]
rules = rules = [Rule(SgmlLinkExtractor(), callback='parse_item', follow=True)]
def parse(self, response):
filename = response.url.split("/")[-1] #so eg it would name 'home'
open(filename, 'wb').write(response.body)
这将生成一个单独的文件,其中包含“主页”的HTML数据 . 如何让它从主页开始递归抓取整个网站?
任何帮助表示赞赏 . 谢谢 .
1 回答
要改变的两件事:
AcdivocaFirstSpider
成为scrapy.contrib.spiders.CrawlSpider
的子类,而不是scrapy.Spider
这句话具有误导性 .
scrapy.Spider
本身没有't do anything special with those start URLs: it downloads them and passes the response'的身体到parse()
. 如果实现parse()
回调以产生进一步的请求,则是,将从这些URL中的数据生成后续URL,但这不是自动/自动的 .scrapy.contrib.spiders.CrawlSpider
时,需要 NOT 覆盖内置的parse()
方法,即检查规则并生成页面请求的方法 . 所以你需要将parse
重命名为parse_item
(在你的规则中引用)见the warning in the docs on crawling rules.