Created
November 28, 2015 05:01
-
-
Save dineshsprabu/f65bc8c40e52420f0dd5 to your computer and use it in GitHub Desktop.
Following Links in Scrapy Spider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scrapy | |
from tutorial.items import DmozItem | |
class DmozSpider(scrapy.Spider): | |
name = "dmoz" | |
allowed_domains = ["dmoz.org"] | |
start_urls = [ | |
"http://www.dmoz.org/Computers/Programming/Languages/Python/", | |
] | |
def parse(self, response): | |
for href in response.css("ul.directory.dir-col > li > a::attr('href')"): | |
url = response.urljoin(href.extract()) | |
yield scrapy.Request(url, callback=self.parse_dir_contents) | |
def parse_dir_contents(self, response): | |
for sel in response.xpath('//ul/li'): | |
item = DmozItem() | |
item['title'] = sel.xpath('a/text()').extract() | |
item['link'] = sel.xpath('a/@href').extract() | |
item['desc'] = sel.xpath('text()').extract() | |
yield item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment