代碼來源:http://mherman.org/blog/2012/11/08/recursively-scraping-web-pages-with-scrapy/#rules 我新來python和scrapy。我搜索了遞歸蜘蛛,發現了這個。Scrapy是否抓取所有規則鏈接?
我有幾個問題:
以下是如何工作的?它只是從頁面獲取href鏈接並將其添加到請求隊列中?
哪個部分的網頁會scrapy抓取?
下面的代碼是否刮掉網頁上的所有鏈接?
可以說,我要爬網,並從該網站http://downloads.trendnet.com/
下載的每一個文件的方式,我可能會做到這一點是湊這個網站上的每一個環節,並檢查URL的內容標題和下載,如果它是一個文件。這是可行的嗎?
很抱歉,如果這是一個糟糕的問題....
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from craigslist_sample.items import CraigslistSampleItem
class MySpider(CrawlSpider):
name = "craigs"
allowed_domains = ["sfbay.craigslist.org"]
start_urls = ["http://sfbay.craigslist.org/search/npo"]
rules = (
Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//a[@class="button next"]',)), callback="parse_items", follow= True),
)
def parse_items(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.xpath('//span[@class="pl"]')
items = []
for titles in titles:
item = CraigslistSampleItem()
item["title"] = titles.xpath("a/text()").extract()
item["link"] = titles.xpath("a/@href").extract()
items.append(item)
return(items)