我正在尝试使用 scrapy 抓取整个网站。根据 scarpy 的文档
start_urls - A list of URLs where the spider will begin to crawl from, when no particular URLs are specified. So, the first pages downloaded will be those listed here. The subsequent URLs will be generated successively from data contained in the start URLs.
所以根据这个定义,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 数据。如何让它从首页开始递归抓取整个网站?
感谢任何帮助。谢谢。
请您参考如下方法:
要改变的两件事:
- 要使用规则,请将
AcdivocaFirstSpider设为scrapy.contrib.spiders.CrawlSpider的子类,而不是scrapy.Spider
The subsequent URLs will be generated successively from data contained in the start URLs.
这个短语具有误导性。 scrapy.Spider 本身不会对这些起始 URL 做任何特殊的事情:它下载它们并将响应的主体传递给 parse()。如果实现 parse() 回调以产生更多请求,那么是的,后续 URL 将从这些 URL 的数据生成,但这不是自动/自动的。 p>
- 当使用
scrapy.contrib.spiders.CrawlSpider时,您需要不覆盖内置的parse()方法,这就是检查规则并生成页面请求。因此,您需要将parse重命名为parse_item(在您的规则中引用)
